A
anujanujdhamija
I am initializing a class variable using a temporary, example:
abc a1, a2;
abc a3 = a1+a2; (See prog below)
I expect a copy constructor to be invoked for initialization of a3. So
in all, I expect 4 constructors and 4 destructors to be invoked for
following program-> one each for a1, a2 and a3 and fourth for temporary
variable in operator+() method.
But to my utter surprise I see only 3 constructors and 3 destructors
getting invoked, one each for a1, a2 and temporary variable. No
constructor or destructor are invoked for a3. (Output at end)
Is it following some different rule for initialization using
temporaries or is it due to some optimization by compiler? If its not
optimization then isn't a3 treaed like a reference to temporary in this
case?
I am compiling it using g++ compiler on cygwin/windows..
/*********************************************************************************/
//Prog.cpp
#include<iostream>
using namespace std;
class abc
{
public:
int p;
string name;
abc(string n):name(n)
{
cout<<"Default"<<endl;
}
abc (const abc &a):name(a.name + "__")
{
cout<<"Copy"<<endl;
}
~abc()
{
cout<<"Destroying:"<<name<<endl;
}
abc& operator =(abc a1)
{
cout<<"Assignment"<<endl;
return *this;
}
};
abc operator +(abc& a1,abc& a2)
{
cout<<"+"<<endl;
abc temp("temp");
temp.p = a1.p + a2.p;
return temp;
}
int main()
{
abc a1("a1");
abc a2("a2");;
abc a3=a1+a2;
return 0;
}
/*********************************************************************************/
/////////////Output:
Default
Default
+
Default
Destroying:temp
Destroying:a2
Destroying:a1
regards
AD
abc a1, a2;
abc a3 = a1+a2; (See prog below)
I expect a copy constructor to be invoked for initialization of a3. So
in all, I expect 4 constructors and 4 destructors to be invoked for
following program-> one each for a1, a2 and a3 and fourth for temporary
variable in operator+() method.
But to my utter surprise I see only 3 constructors and 3 destructors
getting invoked, one each for a1, a2 and temporary variable. No
constructor or destructor are invoked for a3. (Output at end)
Is it following some different rule for initialization using
temporaries or is it due to some optimization by compiler? If its not
optimization then isn't a3 treaed like a reference to temporary in this
case?
I am compiling it using g++ compiler on cygwin/windows..
/*********************************************************************************/
//Prog.cpp
#include<iostream>
using namespace std;
class abc
{
public:
int p;
string name;
abc(string n):name(n)
{
cout<<"Default"<<endl;
}
abc (const abc &a):name(a.name + "__")
{
cout<<"Copy"<<endl;
}
~abc()
{
cout<<"Destroying:"<<name<<endl;
}
abc& operator =(abc a1)
{
cout<<"Assignment"<<endl;
return *this;
}
};
abc operator +(abc& a1,abc& a2)
{
cout<<"+"<<endl;
abc temp("temp");
temp.p = a1.p + a2.p;
return temp;
}
int main()
{
abc a1("a1");
abc a2("a2");;
abc a3=a1+a2;
return 0;
}
/*********************************************************************************/
/////////////Output:
Default
Default
+
Default
Destroying:temp
Destroying:a2
Destroying:a1
regards
AD