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.