temporaries and copy constructor.

Hi all ,

suppose that function that takes object A , like this ,
and another function that creates and return a temporary object or A.

1
2
3
4
5
6
7
8
9
10
11
A CreateA ( )
{
          A a ;
          return a;
}


void takeA(const A a )
{
     
}


Oky when I call like this , the copy constructor is calling.

1
2
3
4
5
6
7
8
9
10
11
12
13
int main(int argc, char *argv[])
{
    A a ;
    
    takeA(a);
    // a temporary object has been created.
    // and it was passed to the functione.
        
    cout << "Comes to here " << '\n';
    delete &a;
    system("PAUSE");
    return EXIT_SUCCESS;
}


But I call like this , the copy constructor is not calling. tell me what is the
magic that is going behind the screen ?
1
2
3
4
5
6
7
8
9
10
11
int main(int argc, char *argv[])
{
   takeA(CreateA());
    // a temporary object has been created.
    // and it was passed to the functione.
        
    cout << "Comes to here " << '\n';

    system("PAUSE");
    return EXIT_SUCCESS;
}

then the copy constructor is not calling..... My complete source code is .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <fstream>

using namespace std;

class A
{
public:      
      // the constructor
      A()
      {
             cout<< " The constructor called " << '\n';
      }
      
      // the copy constructor
      A(const  A& a )
      {
            cout << " The copy constructor is called" << '\n';
      }
      
      ~A()
      {
          cout << "The destructor is called "<< '\n';          
      }
};

A CreateA ( )
{
          A a ;
          return a;
}


void takeA(const A a )
{
     
}
int main(int argc, char *argv[])
{
   takeA(CreateA());
    // a temporary object has been created.
    // and it was passed to the functione.
        
    cout << "Comes to here " << '\n';
   
    system("PAUSE");
    return EXIT_SUCCESS;
}


so is that means that temporary objects are not call by value , they are just
refering ? so what is the rule ?
I need to know how the machine code is generated for the source given bellow.

Thanks in advance.
What're the symptoms of the problem? How do you know the problem's in the copy constructor?
For the line takeA(CreateA()); it seems that whether the
temp variable returned by CreateA() is copied or not, before being passed to takeA() is compiler dependent.

Micorsoft copies it, GCC does not.
It is called return value optimization (RVO).

The compiler knows that the stack space used by the "a" temporary in CreateA() can just remain allocated so that takeA can be called with that temporary instead of copy constructing it.

It is called return value optmization (RVO).


wow , so there is something that I have to read about.jsmith can you direct me
some link or somewhere to read about the rest.

for all thanks in advance.......
Try here:

http://www.efnetcpp.org/wiki/Return_value_optimization

(I just googled "return value optimization").
Topic archived. No new replies allowed.