简体   繁体   中英

Can I call a derived class constructor from a base class?

If I have a base class.

class Base{
   ...
};

and a derived class

class Derived : public Base{
   ...
}

Can I call the Derived class constructor from the main() class, if I only include Base class ?

To clarify what I mean. Can I do this?

#include "Base.h"

int main(){
  Derived d;
}

Note: The only answers I was able to find were if I can call Base constructor from Derived class, but I want to do the opposite.

I only include "Base.h". Do I have to include "Derived.h" to call the constructor? What if I had 150 subclasses under one superclass? Do I have to include each one ?

You cannot call a derived class constructor from a base class. The base class has no idea the derived class even exist.

Constructors have a special job of initializing the object properly. A Derived class constructor has access only to its own class members, but a Derived class object also have inherited property of Base class, and only base class constructor can properly initialize base class members. Hence all the constructors are called, else object wouldn't be constructed properly.source:

https://www.studytonight.com/cpp/order-of-constructor-call.php

Whenever the derived class's default constructor is called, the base class's default constructor is called automatically.Whenever the derived class inherited base class Constructors couldn't inherited in any mode(public,private,protected)

You derived class should include Base.h since it is needed for compilation. You then can include Derived in your main file.

When a derived object is constructed, it calls the base default constructor automatically. If you don't want it to call the base default constructor and call a non-default constructor, then you do something like this:

Derived::Derived() : Base(/*Parameters to call base non-default constructor here*/)
{
    //Do stuff specifically for the derived class
}

If you don't include any constructors at all, then the compiler will add one for you (basically setting all the variables to the compiler's default values.

The order in which these occur are

  1. Base Constructor (Non-default if you specify like above)
  2. Derived Constructor

Also, with your includes, any object that you are going to refer to in main must be included.

There are a number of misconceptions in the question:

  1. You never "call a constructor". Syntax to do this does not exist. What you are trying to do is to declare, define, instantiate, create an object of type Derived .

  2. You're attempt to do so from main() , not from the base class.

  3. main() is a function, not a class.

  4. No, you can't create a Derived without its definition. That its base is in scope is insufficient, and even if it weren't, the compiler has no way of knowing that a base is in scope, because it doesn't know what Derived is.

Can I call a derived class constructor from a base class?

If you mean "Can I create derived class instances in a member function of a base class" , then yes you can. An example:

struct Base {
    void foo();
};

struct Derived {
    Derived() {
        // a constructor
    }
};

void Base::foo() {
    Derived d; // "calling the constructor" of derived class
}

Exactly when would this be useful? Probably never.


Can I call the Derived class constructor from the main class, if I only include Base class ?

No. You only create instances of classes (I assume that's what you mean by "call the constructor") that have been defined. If you don't include the definition, then you cannot create instances.

Do I have to include "Derived.h" to call the constructor?

If that's where the class is defined, then yes. The class must be defined before it can be instantiated.

What if I had 150 subclasses under one superclass? Do I have to include each one ?

If you're going to instantiate objects of 150 classes then yes, all 150 classes must be defined first. However, I suspect that's not something you need to do.

To clarify what I mean. Can I do this?

 #include "Base.h" int main(){ Derived d; } 

Depends on what "Base.h" contains. If it contains the definition of the Derived class (which would be unconventional), then yes. If it doesn't contain the definition of Derived (which would be conventional), then no.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM