L
Lloyd
Hi,
This is a small code piece I have written to test. It is overloading "operator=". To my surprise this code prints "base" always! If I overload "operator+" it prints "derived". Can you give a reason why "=" behaves that way in this code?
#include <iostream>
using std::cout;
class derived;
class base
{
public:
virtual derived& operator=(const base& rhs);
};
class derived: public base
{
public:
derived& operator=(const base& rhs);
};
derived x;
derived& base:
perator=(const base& rhs)
{
cout<<"base\n";
return x;
}
derived& derived:
perator=(const base& rhs)
{
cout<<"derived\n";
return x;
}
int main()
{
derived d,e;
d=e;
return 0;
}
Thanks,
Lloyd
This is a small code piece I have written to test. It is overloading "operator=". To my surprise this code prints "base" always! If I overload "operator+" it prints "derived". Can you give a reason why "=" behaves that way in this code?
#include <iostream>
using std::cout;
class derived;
class base
{
public:
virtual derived& operator=(const base& rhs);
};
class derived: public base
{
public:
derived& operator=(const base& rhs);
};
derived x;
derived& base:
{
cout<<"base\n";
return x;
}
derived& derived:
{
cout<<"derived\n";
return x;
}
int main()
{
derived d,e;
d=e;
return 0;
}
Thanks,
Lloyd