I have this class hierarchy where I'm trying to add operator= :
class A
{
public:
virtual void someFunction() = 0;
virtual A& operator=(const A&) = 0;
};
class B : public A
{
public:
void someFunction() {
//implementation
}
A& operator=(const A& o)
{
*ptr = *o.ptr;
return *this;
}
private:
A* ptr;
};
class C : public A
{
public:
void someFunction() {
//implementation
}
A& operator=(const A& o)
{
data = o.data;
return *this;
}
private:
int data; //NOTE: different members that needs to be copied in the operator
};
I understand why this doesn't work. I have a private member in B (that needs to be there) and a function A&operator=(const A&)
that needs to be overwritten. Problem is that o
is of type A and doesn't have the pointer ptr
.
I've tried to dynamic_cast o to type B, but
- that wont work since it's constant,
- It seems unsafe (if rhs is of type C)
Same issue for class C. Is there some cleaver work-around?
Clarification of why I need it this way:
class superClass
{
public:
superClass& operator=(const superClass& o)
{
*some_A_type = *o.some_A_type;
}
private:
A* some_A_type;
};
essentially, what I want is an operator= for superClass. I'm not sure where or how to fix it.