L
lali.cpp
Hi,
Sorry for the stupid subject(i could not think of anything better).
Why is the following code not compilable:
class Example
{
public:
// friend Example operator+(const Example&,const Example&);
Example()
{
}
Example(int n):m(n)
{
}
Example operator+(const Example& ex) const
{
cout<<"\nClasss overload called"<<endl;
return Example(m+ex.m);
}
private:
int m;
};
/*Example operator+(const Example& ex1,const Example& ex2)
{
return Example(ex1.m+ex2.m);
}*/
int main()
{
Example ex1;
Example ex2=1+ex1;
return 0;
}
What i expect is that 1+ex1 should actually be interpreted as Example
(1)+ex1(the 1 argument constructor is not explicit) and then the
operator+ function for the temporary object(i.e Example(1)) should be
called.
May be there is some rule regarding temporary objects that i am
missing. Kindly elaborate why is it illegal ?
Regards
Kartik Mahajan
Sorry for the stupid subject(i could not think of anything better).
Why is the following code not compilable:
class Example
{
public:
// friend Example operator+(const Example&,const Example&);
Example()
{
}
Example(int n):m(n)
{
}
Example operator+(const Example& ex) const
{
cout<<"\nClasss overload called"<<endl;
return Example(m+ex.m);
}
private:
int m;
};
/*Example operator+(const Example& ex1,const Example& ex2)
{
return Example(ex1.m+ex2.m);
}*/
int main()
{
Example ex1;
Example ex2=1+ex1;
return 0;
}
What i expect is that 1+ex1 should actually be interpreted as Example
(1)+ex1(the 1 argument constructor is not explicit) and then the
operator+ function for the temporary object(i.e Example(1)) should be
called.
May be there is some rule regarding temporary objects that i am
missing. Kindly elaborate why is it illegal ?
Regards
Kartik Mahajan