Not calling base class constructor from derived class

Brad picture Brad · Oct 31, 2010 · Viewed 13.4k times · Source

Say I have a base class:

class baseClass  
{  
  public:  
baseClass() { };

};

And a derived class:

class derClass : public baseClass
    {  
      public:  
    derClass() { };

    };

When I create an instance of derClass the constructor of baseClass is called. How can I prevent this?

Answer

Alexey Malistov picture Alexey Malistov · Oct 31, 2010

Make an additional empty constructor.

struct noprapere_tag {};

class baseClass  
{  
public:  
  baseClass() : x (5), y(6) { };

  baseClass(noprapere_tag) { }; // nothing to do

protected:
  int x;
  int y;

};

class derClass : public baseClass
{  
public:  
    derClass() : baseClass (noprapere_tag) { };

};