GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 17-Jan-2006, 12:59
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 172
Blake will become famous soon enough

copy constructor and return values


Suppose I have a function as follows:

return_type func( ... args ... )
{
return_type T;
.
.
.
return T;
}

The line "return T;" will call the copy constructor for the class return_type.

Now suppose I have the following line of code:

return_type T(func(... args ...));

Again, the copy contructor is being called to initialize T. Does that mean that the copy contructor will be called twice, or will the compiler recognize this and deal with it?

I'm using Borland C++ Builder X, Windows XP, if it matters.
  #2  
Old 17-Jan-2006, 14:31
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: copy constructor and return values


Quote:
Originally Posted by Blake
Now suppose I have the following line of code:
CPP / C++ / C Code:
return_type T(func(... args ...));
Does that mean that the copy contructor will be called twice,

Why don't you make a simple test program and see what happens? Maybe something like:
CPP / C++ / C Code:
#include <iostream>

using namespace std;

class Cl
{ public:
      Cl(){cout << "constructor Cl()" << endl; index = 0;};
      Cl(int x) {cout << "constructor Cl(" << x << ")" << endl; index = x;};
      ~Cl() {};

      int index;
};

int main()
{
  cout << "beginning of main()" << endl;
  Cl func(int x); // prototype

  cout << "before \"Cl myclass_func(10)\"" << endl;
  Cl my_class(func(10)); // instantiate an object

  cout << "my_class.index = " << my_class.index << endl;

  return 0;
}

Cl func(int x)
{
  cout << "beginning of  func(" << x << ")" << endl;
  Cl T;
  cout << "returning from func()" << endl;
  return T;
}

My output:

Code:
beginning of main() before "Cl myclass_func(10)" beginning of func(10) constructor Cl() returning from func() my_class.index = 0

Now, I'm not sure that this is exactly what you had in mind, but that is kind of the point: You have two wonderful tools at your own location. I will list them in reverse alphabetical order:

1. Your compiler
2. Your brain

The other important tool is somewhat remote (a programming assistance forum on the internet). It's always OK to ask, but sometimes it is faster (and more edifying) to explore on your own. (Faster, I claim, than posting a request for help and waiting for a helpful response.)

Regards,

Dave

"The little gray cells, Hastings; the little gray cells."
---Hercule Poirot
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:54.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.