Home > Net >  derived constructor without calling base constructor
derived constructor without calling base constructor

Time:12-03

I'm new to OOP and I happened to know that when a constructor (or deconstructor) of a derived class is called, a constructor (or deconstructor) of a base class is also called. But what if I don't want to have base constructor/ deconstructor called then what can I do?

class Base{
    public:
    
    Base(){
        cout<<"Base constructor called\n";
    }
    ~Base(){
        cout<<"Base deconstructor called\n";
    }
};
class Derived: public Base{
    public:
    Derived(){
        cout<<"Derived constructor called\n";
    }
    ~Derived(){
        cout<<"Derived deconstructor called\n";
    }
};
int main()
{
    Derived* obj_a = new Derived;
    delete obj_a;

    return 0;
}

the result is:

Base constructor called
Derived constructor called
Derived deconstructor called
Base deconstructor called

CodePudding user response:

This is not java; base ctor/dtor will always be called, as the invariant of base must be established and the operations required to destroy a class must be performed. If you need to somehow signal from descendant to base that some specific resources should not be freed, you need to make it a part of the base's state; but usually it's considered a bad pattern.

CodePudding user response:

Part of the point of inheritance is that an instance of a derived class is also an instance of the base class. Therefore, when you create a derived object, the base class constructor always runs to establish this as an instance of the base class. Then the derived constructor runs to also establish it as an instance of the derived class.

If you don't want the base class constructor to run, that's fairly directly saying that either the base class constructor is doing things it shouldn't be, or else you don't want this to be an object of the base class after all.

If the problem is with the base class constructor, you obviously fix that.

If the problem is that you don't want your derived object to be an instance of the base class, you have a couple of possible approaches, depending on the precise source of the problem.

One fairly common problem is that the derivation initially seems reasonable, but really isn't. Many people tend to think in terms of similarity. If one thing is 95% like another, with a few things added on, we often think inheritance can be used to represent that reasonably. It can't. We may not be able to use derivation at all, or we may have to create some third class that contains only the 95% that's alike, to act as the base of both of the classes involved. But for things to work well, a derived class must be a strict superset of the base class--it must share 100% of the base class, and add some other "stuff" as well.

Inheritance is misused and abused so often that when I see a question like this, without a description of the specific problem, my first guess is that you're probably misusing inheritance, so this probably isn't just a matter of fixing the base class constructor, it's a matter of needing a different hierarchy, or (more likely) shouldn't really be using inheritance at all.

  • Related