Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't a derived class use the base class operator= (assignment operator)?

Following is a simplified version of an actual problem. Rather than call Base::operator=(int), the code appears to generate a temporary Derived object and copy that instead. Why doesn't the base assignment operator get used, since the function signature seems to match perfectly? This simplified example doesn't display any ill effects, but the original code has a side-effect in the destructor that causes all kinds of havoc.

#include <iostream>
using namespace std;

class Base
{
public:
   Base()
   {
      cout << "Base()\n";
   }

   Base(int)
   {
      cout << "Base(int)\n";
   }

   ~Base()
   {
      cout << "~Base()\n";
   }

   Base& operator=(int)
   {
      cout << "Base::operator=(int)\n";
      return *this;
   }
};

class Derived : public Base
{
public:
   Derived()
   {
      cout << "Derived()\n";
   }

   explicit Derived(int n) : Base(n)
   {
      cout << "Derived(int)\n";
   }

   ~Derived()
   {
      cout << "~Derived()\n";
   }
};

class Holder
{
public:
   Holder(int n)
   {
      member = n;
   }

   Derived member;
};

int main(int argc, char* argv[])
{
   cout << "Start\n";
   Holder obj(1);
   cout << "Finish\n";

   return 0;
}

The output is:

Start
Base()
Derived()
Base(int)
Derived(int)
~Derived()
~Base()
Finish
~Derived()
~Base()

http://ideone.com/TAR2S

like image 283
Mark Ransom Avatar asked May 31 '12 17:05

Mark Ransom


People also ask

Does derived class inherit assignment operator?

In C++, like other functions, assignment operator function is inherited in derived class.

Can we assign derived class object to base?

In C++, a derived class object can be assigned to a base class object, but the other way is not possible.

What is not inherited by a derived class from base class?

Following are the properties which a derived class doesn't inherit from its parent class : 1) The base class's constructors and destructor. 2) The base class's friend functions. 3) Overloaded operators of the base class.

What is not used automatically for the derived class from a base class?

A derived Java class does not inherit a constructor from its base class. If a base class has a default constructor, i.e., a constructor with no arguments, then that constructor is automatically called when a derived class is instantiated if the derived class has its own default constructor.


1 Answers

This is a subtle interaction between a compiler-generated operator= method and member function hiding. Since the Derived class did not declare any operator= members, one was implicitly generated by the compiler: Derived& operator=(const Derived& source). This operator= hid the operator= in the base class so it couldn't be used. The compiler was still able to complete the assignment by creating a temporary object using the Derived(int) constructor and copy it with the implicitly generated assignment operator.

Because the function doing the hiding was generated implicitly and wasn't part of the source, it was very hard to spot.

This could have been discovered by using the explicit keyword on the int constructor - the compiler would have issued an error instead of generating the temporary object automatically. In the original code the implicit conversion is a well-used feature, so explicit wasn't used.

The solution is fairly simple, the Derived class can explicitly pull in the definition from the Base class:

using Base::operator=;

http://ideone.com/6nWmx

like image 133
Mark Ransom Avatar answered Oct 12 '22 01:10

Mark Ransom