I have code like this:
class Base
{
public:
void operator = (const Base& base_)
{
}
};
class Child : public Base
{
public:
};
void func()
{
const Base base;
Child child;
child = base;
}
My question is: since Child derives from Base (hence it should inherit Base's operator= ), how come when the statement
child = base;
is executed, I get a compiler error like this:
>.\main.cpp(78) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const Base' (or there is no acceptable conversion)
1> .\main.cpp(69): could be 'Child &Child::operator =(const Child &)'
1> while trying to match the argument list '(Child, const Base)'
The behavior that I want is for the Child class to recognize that it's being assigned a Base class, and just "automatically" call the its parent's operator=.
Once I added this code to the Child class
void operator = (const Base& base_)
{
Base::operator=(base_);
}
then everything compiled fine. Though I dont think this would be good because if I have like 5 different classes that inherit from Base, then I have to repeat the same code in every single derived class.
NOTE: My intention for copying the Base
to Child
is to simply copying the members that are common to both Base
and Child
(which would be all the members of Base
). Even after reading all of the answers below, I really don't see why C++ doesn't allow one to do this, especially if there's an explicit operator=
defined in the Base
class.
No, that's not possible since assigning it to a derived class reference would be like saying "Base class is a fully capable substitute for derived class, it can do everything the derived class can do", which is not true since derived classes in general offer more functionality than their base class (at least, that's ...
In real world, parents can accommodate children but children cannot accommodate parents. same is the case in OOP. class Parent { int prop1; int prop2; } class Child : Parent // class Child extends Parent (in case of Java Lang.)
In Simple Terms, Objects of Parent class can hold objects of child class.
The parent class can hold reference to both the parent and child objects. If a parent class variable holds reference of the child class, and the value is present in both the classes, in general, the reference belongs to the parent class variable. This is due to the run-time polymorphism characteristic in Java.
The standard provides the reason for your specific question in 12.8/10 "Copying class objects" (emphasis added):
Because a copy assignment operator is implicitly declared for a class if not declared by the user, a base class copy assignment operator is always hidden by the copy assignment operator of a derived class (13.5.3).
So since there's an implicitly declared operator=(const Child&)
when the compiler is performing the name lookup/overload resolution for a Child::operator=()
, the Base
class's function signature is never even considered (it's hidden).
The code below is the behavior I wanted since the beginning, and it compiles,
class Base
{
public:
void operator = ( const Base& base_)
{
}
};
class Child : public Base
{
};
void func()
{
const Base base;
Child child;
child.Base::operator=(base);
}
I never knew that you can explicitly call something like:
child.Base::operator=(base);
Anyway, I learned a lot. Thank you for all the people that posted answers here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With