18

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?

Brad
  • 10,015
  • 17
  • 54
  • 77
  • 1
    You don't; that doesn't make sense. Why do you think you need to do this? – GManNickG Oct 31 '10 at 21:05
  • @GMan I have a base class where the data is prepared with a header in the constructor. The derived class doesn't need a header prepared, but it's calling the base class constructor which is preparing a header and messes some things up. – Brad Oct 31 '10 at 21:10
  • 6
    You're missing the point of inheritance. Inheritance forms an "an A is a B" relationship. If a B does things that an A should not do, then an A is not a B. – Benjamin Lindley Oct 31 '10 at 21:17
  • Please read [this](http://www.catb.org/esr/faqs/smart-questions.html). Ask about the *whole problem you're trying to solve*, not the step you think you need to take. – GManNickG Oct 31 '10 at 21:44
  • 1
    change the base class, not the derived class. – Dialecticus Oct 31 '10 at 21:48

4 Answers4

21

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) { };

};
PunyCode
  • 373
  • 4
  • 18
Alexey Malistov
  • 26,407
  • 13
  • 68
  • 88
9

A base class instance is an integral part of any derived class instance. If you successfully construct a derived class instance you must - by definition - construct all base class and member objects otherwise the construction of the derived object would have failed. Constructing a base class instance involves calling one of its constructors.

This is fundamental to how inheritance works in C++.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
1

Sample working program

#include <iostream>

using namespace std;
class A
{
    public:
    A()
    {
        cout<<"a\n";
    }
    A(int a)
    {}
};
class B:public A
{
    public:
    B() : A(10)
    {
        cout<<"b\n";
    }
   
};
int main()
{
    
    new A;
    cout<<"----------\n";
    new B;
    
    return 0;
}

output

a                                                                                                        
----------                                                                                               
b   
Sathvik
  • 565
  • 1
  • 7
  • 17
0

You might want to create a protected (possibly empty) default constructor which would be used by the derived class:

class Base {
protected:
  Base() {}

public:
  int a;
  
  Base(int x) {a = 12;}
};

class Derived : Base {
public:
  Derived(int x) {a = 42;}
};

This will block any external code from using default Base constructor (and thus not initializing a properly). You just need to make sure you initialize all the Base fields in the Derived constructor as well.

Jindra Helcl
  • 3,457
  • 1
  • 20
  • 26