Reputation: 2241
class A
{
int Avalue;
A& operator=(A& copyMe)
{
Avalue = copyMe.Avalue;
return *this;
}
}
class B:public A
{
int Bvalue;
B& operator=(B& copyMe)
{
Bvalue = copyMe.Bvalue
return *this;
}
}
How to invoke the A or the base class assignment operator from the B's assignment operator?.How to handle A's return reference from the assignment operator call.
Upvotes: 1
Views: 198
Reputation:
All operators can be thought of as a bit of clever functions, but that's just courtesy of the compiler which is smart enough to infer what are the given operands and pass them into your override. It's a great extensible way of adding nice syntactic sugar in situations where it makes sense, but because of their usual usage, people cannot really see that this is valid, but when you observe your definitions of the overrides, they look like functions and act like functions. So, simply use the scope operator ::
to access the public parts of A's interface.
A::operator=(copyMe)
Do note you're missing several semicolons and also, classes default to private
access. To gain access to the copy assignment operator in A, you need to specify it as public:
class A
{
private:
int Avalue;
public:
A& operator=(A& copyMe)
{
Avalue = copyMe.Avalue;
return *this;
}
}
Upvotes: 2
Reputation: 3293
Call the assignment operator (or any other function) explicitly using the scope operator: A::operator=( someval );
Check out: http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B
Upvotes: 1
Reputation: 146910
You just need to add A::operator=(copyMe)
in the derived op. Also, copies should be of non-const reference.
Upvotes: 0