0

I have a class which inherits from a base class which provides a protected constructor which is empty.

Is it necessary for me to implement the blank constructor (and destructor) in the derived class or will the compiler generate the appropriate one for me. I am using C++ 11.

While some aspacts of the questions are answered in this post (How is "=default" different from "{}" for default constructor and destructor?), I am mostly interested in the behaviour when the class is derived.

So I have something like:

template<typename EnumClass>
class IVCounter
{
protected:
    //!
    //! \brief Ensure that this base class is never instantiated
    //! directly.
    //!
    IVCounter() {}


public:
    //!
    //! \brief Virtual destructor
    //!
    virtual ~IVCounter() {}
};

class Derived: public IVCounter<SomeType>
{
   // Do I have to do this?
   Derived() 
   : IVCounter()
   {}

   ~Derived() {}
};

Or perhaps in the derived I can simply do:

Derived() = default;
~Derived() = default;

or maybe even leave it out altogether?

Luca
  • 10,458
  • 24
  • 107
  • 234
  • Possible duplicate of [How is "=default" different from "{}" for default constructor and destructor?](https://stackoverflow.com/questions/13576055/how-is-default-different-from-for-default-constructor-and-destructor) – 463035818_is_not_an_ai May 09 '18 at 08:27
  • That is a good thread but I am mostly interested in the behaviour when the class is derived. – Luca May 09 '18 at 08:29
  • Yes compiler will generate one for you. What are you worried about? – songyuanyao May 09 '18 at 08:29
  • I am worried if the base class constructor will be called and the class instance will be initialized correctly – Luca May 09 '18 at 08:30
  • This article may help you :) http://www.learncpp.com/cpp-tutorial/114-constructors-and-initialization-of-derived-classes/ – BartekPL May 09 '18 at 08:36
  • in your case it makes no difference that you have inheritance. It is similar to having a member that has a default constructor. Also the `IVCounter` constructor being protected is a red herring, of course the derived can access it – 463035818_is_not_an_ai May 09 '18 at 08:36
  • ...ah only now I fully understand your question. I would suggest you not to mix to many things at once. "How to correctly call the base class constructor?" is a slightly different question than "Can I use `=default` or can I rely on the compiler to create the default constructor?" – 463035818_is_not_an_ai May 09 '18 at 08:38
  • @Luca When constructing derived class base class subobject is guraanteed to be constructed. – songyuanyao May 09 '18 at 08:40
  • `Do I have to do this?` You can `cout` from Base constructor and try with and without to answer your question, but make sure you make Derived constructor `public` first; – Killzone Kid May 09 '18 at 08:45

3 Answers3

1

Yes. compiler will generate blank constructor, you need not.

code707
  • 1,663
  • 1
  • 8
  • 20
1

Default generated constructor would be public, so following is enough

class Derived: public IVCounter<SomeType>
{
};
Jarod42
  • 203,559
  • 14
  • 181
  • 302
1

You do not need an explicit constructor here. The implicit default constructor is enough. Draft N4659 says at 15.6.2 Initializing bases and members [class.base.init] § 13:

In a non-delegating constructor, initialization proceeds in the following order:

  • First, and only for the constructor of the most derived class (4.5), virtual base classes are initialized in the order they appear on a depth-first left-to-right traversal of the directed acyclic graph of base classes, where “left-to-right” is the order of appearance of the base classes in the derived class base-specifier-list.
  • Then, direct base classes are initialized in declaration order as they appear in the base-specifier-list (regardless of the order of the mem-initializers).
  • Then, non-static data members are initialized in the order they were declared in the class definition (again regardless of the order of the mem-initializers).
  • Finally, the compound-statement of the constructor body is executed.

The implicitely default constructor has just an empty body, but construction of an object implies construction of its base classes

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252