Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ How come I can't assign a base class to a child class?

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.

like image 401
sivabudh Avatar asked Feb 11 '10 02:02

sivabudh


People also ask

Why can't we assign a base class object to a derived class variable?

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 ...

Can we assign parent object to child objects in C#?

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.)

Can we assign a parent object to a child?

In Simple Terms, Objects of Parent class can hold objects of child class.

Can we assign object of parent class to 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.


2 Answers

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).

like image 91
Michael Burr Avatar answered Sep 24 '22 21:09

Michael Burr


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.

like image 45
sivabudh Avatar answered Sep 23 '22 21:09

sivabudh