Inheritance and operator overloading
With the following piece of code:
does anyone know why when the obj3 = obj1 + obj2 line is executed it uses the derived operator=() but when
obj3 = obj1 is executed it uses the base operator=() ?
Cheers.
EDIT: Not to worry, I worked it out in the end. :roll:
#include<iostream>
using namespace std;
class base {
public:
base() {; }
base operator+(base ob2)
{
base temp;
return temp;
}
base operator=(base ob2)
{
cout << "Using base operator=() " << '\n';
return *this;
}
};
class derived : public base {
public:
derived() { ; }
derived operator=(base ob2)
{
cout << "Using derived operator=() " << '\n';
}
};
int main()
{
derived obj1, obj2, obj3;
obj3 = obj1 + obj2; // Uses derived operator=()
obj3 = obj1; // Uses base operator=()
return 0;
}
does anyone know why when the obj3 = obj1 + obj2 line is executed it uses the derived operator=() but when
obj3 = obj1 is executed it uses the base operator=() ?
Cheers.
EDIT: Not to worry, I worked it out in the end. :roll:
