S
sanjay
Hi All,
I have a doubt in understanding the output of the following program
that i executed on my system. I was using DevC++ IDE which uses minGW
based compiler.
----------------------------------------------
#include <iostream>
using namespace std;
class Integer //: public DataType
{
private:
int x;
public:
Integer(int xx = 0);
Integer(const Integer&);
~Integer();
Integer operator+(const int i);
void operator=(const Integer& i);
};
Integer::Integer(int xx) : x(xx)
{
cout<<"Integer(int) x is "<<x<<endl;
}
Integer::Integer(const Integer& i) : x(i.x)
{
cout<<"Integer(Integer&)"<<endl;
}
Integer::~Integer()
{
cout<<"~Integer()"<<endl;
}
Integer Integer:perator+(const int i)
{
cout<<"operator+ called i is "<<i<<endl;
Integer xx(x+i);
return xx;
}
void Integer:perator=(const Integer& i)
{
cout<<"operator= called i.x is "<<i.x<<endl;
x = i.x;
}
int main()
{
Integer i1(40);
Integer i3=i1+10; //Doubt understanding this
return 0;
}
-----------------------------------
Output:
Integer(int) x is 40
operator+ called i is 10
Integer(int) x is 50
~Integer()
~Integer()
-------------------------------------
I have a doubt in understanding the execution of operator+.
Inside operator+, xx is created and is returned by value. While
returning the copy-constructor of Integer should get invoked because
the output of operator+ is being assigned to i3 which is not yet
constructed, but the actual output observed is different.
Is this any kind of optimization being performed by the compiler. Is
there anything that i am missing.
I would appreciate if someone can explain this.
Regards
Sanjay Raghani
I have a doubt in understanding the output of the following program
that i executed on my system. I was using DevC++ IDE which uses minGW
based compiler.
----------------------------------------------
#include <iostream>
using namespace std;
class Integer //: public DataType
{
private:
int x;
public:
Integer(int xx = 0);
Integer(const Integer&);
~Integer();
Integer operator+(const int i);
void operator=(const Integer& i);
};
Integer::Integer(int xx) : x(xx)
{
cout<<"Integer(int) x is "<<x<<endl;
}
Integer::Integer(const Integer& i) : x(i.x)
{
cout<<"Integer(Integer&)"<<endl;
}
Integer::~Integer()
{
cout<<"~Integer()"<<endl;
}
Integer Integer:perator+(const int i)
{
cout<<"operator+ called i is "<<i<<endl;
Integer xx(x+i);
return xx;
}
void Integer:perator=(const Integer& i)
{
cout<<"operator= called i.x is "<<i.x<<endl;
x = i.x;
}
int main()
{
Integer i1(40);
Integer i3=i1+10; //Doubt understanding this
return 0;
}
-----------------------------------
Output:
Integer(int) x is 40
operator+ called i is 10
Integer(int) x is 50
~Integer()
~Integer()
-------------------------------------
I have a doubt in understanding the execution of operator+.
Inside operator+, xx is created and is returned by value. While
returning the copy-constructor of Integer should get invoked because
the output of operator+ is being assigned to i3 which is not yet
constructed, but the actual output observed is different.
Is this any kind of optimization being performed by the compiler. Is
there anything that i am missing.
I would appreciate if someone can explain this.
Regards
Sanjay Raghani