-1

I have an abstract class, and its derived classes.

I want to have a member (i.e. fDerivedName) at the abstract class with the name of the derived class.

In the constructor of each derived class I do :

fDerivedName = this->ClassName();

There is a way to avoid the re-implementation of this in each derived class?

Thanks

Javier Galan
  • 19
  • 2
  • 5

1 Answers1

1

You can implement a constructor in the base class (which is then not pure abstract anymore), pass the name of the derived class as parameter and do your assignment in the initializer list of the constructor:

class Base
{
public:
   Base( const std::string& derived_class_name):
      fDerivedName( derived_class_name)
   {
   }
   ...
};

But, of course, you still have to call this base class constructor from each constructor of your derived classes. And you will have to pass the class name as parameter, you cannot e.g. call a virtual function ClassName() overloaded by the derived classes.

Rene
  • 2,466
  • 1
  • 12
  • 18