#include<iostream>
using namespace std;
class rational
{
public:
rational(int m , int n = 1) { x = m; y = n; cout<<"cons"<<endl;}
rational(const rational & r) { cout<<"copy"<<endl; }
const rational operator +(const rational &r);
private:
int x;
int y;
};
const rational rational::operator +(const rational &r)
{
return rational(x + r.x , y + r.y);
}
int main()
{
rational r1(2,3);
rational r2(4);
rational r = r1 + r2;
return 0;
}
The above code is compiled successfully using gcc compiler.
I am quite surprised to note that neither constructor or copy constructor is being called when executing statement rational r = r1 + r2;
since here a temporary object is being create due to r1+r2 and that temporary object is being assigned to r , so either construtor or copy constructor should be called by compiler.but its not calling.WHY?
-ravi
Check out Yahoo! India Rakhi Special for Rakhi shopping, contests and lots more.
http://in.promos.yahoo.com/rakhi/index.html