#include <iostream>
class aa
{
public:
aa(){}
aa(aa& obj)
{
aa1 = obj.aa1;
}
virtual aa operator =(aa& obj)
{
aa1 = obj.aa1;
return (*this);
}
virtual aa operator +(aa& obj)
{
aa1 += obj.aa1;
return (*this);
}
int aa1;
};
class bb: public aa
{
public:
bb():aa(){}
bb(bb& obj)
{
bb1 = obj.bb1;
}
aa operator =(aa& obj)
{
aa::operator =(obj);
bb b1 = dynamic_cast<bb&>(obj);
bb1 = b1.bb1;
return (*this);
}
aa operator +(aa& obj)
{
aa::operator +(obj);
bb b1 = dynamic_cast<bb&>(obj);
bb1 += b1.bb1;
return (*this);
}
int bb1;
};
int main()
{
bb b1;
bb b2;
b1.bb1 = 1;
b1.aa1 = 1;
b2.bb1 = 2;
b2.aa1 = 2;
aa &a1 = b1;
aa &a2 = b2;
a1 = a2;
b1 = dynamic_cast<bb&>(a1);
b2 = dynamic_cast<bb&>(a2);
std::cout<<b1.aa1<<";"<<b1.bb1;
bb b3;
b3.bb1 = 3;
b3.aa1 = 3;
aa &a3 = b3;
aa &a4 = a2 + a3;
b3 = dynamic_cast<bb&>(a4);
return 0;
}
Output:
2;2 and then it crashes at line b3 = dynamic_cast<bb&>(a4); giving the error std::bad_cast at memory location 0x0012fdbc..
The reason I have found is the result of expression of a2+a3 is coming as object of type aa, and in the next statement we are trying to cast it to derived type object which is not valid, hence leading to exception. So my query is can we achieve the above intention i.e. aa &a4 = a2 + a3; with some changes in the above functions?
C++ has a single dispatch (based on virtual function).
Polymorphic binary operators fall inevitably into the “dual dispatch” case, and hence cannot be implemented polymorphicaly by means of just simple virtual function.
Also, since you are returning a value, every sub-classes information is lost.
A more proper way to handle this kind of situation is to define a non-polymorphic handle that implements the operations and holds polymorphic types, delegating to them the operation execution.
Like