0

I just want to do some adjusts before launching the parent's creator. Is it possible? If no, how would you solve this?

#include <iostream>
using namespace std;

class vehicle
{
    protected:
    string type = "vehicle"; 
    public:
    vehicle() { cout << type << " created" <<endl; }
    void what_am_i() { cout << "I am a " << type <<endl;}
};

class car: public vehicle
{
    public:
    car() 
    { 
        type = "car";
        //I would like to launch vehicle() here ;
    }
};

int main()
{
    car Lorean;
    Lorean.what_am_i();
}

Current result:

vehicle created
I am a car

Wanted result:

car created
I am a car

PD: The constructor output is just an example. I need to launch some processes in the parent's creator which use many attributes that must to updated in each child class. The process are exactly the same in all of them, so It would be wonderful that all of them run in the parent creator.

PPD: To illustrate what I hoped to do with C++, please look what I have done with Python3. Here parent's creator can be launched any time, even never (this behaviour seems to be imposible with C++):

class vehiculo:
    tipo =  "vehiculo"
    def __init__(self): print(self.tipo.capitalize() ,"creado.")
    def que_soy(self): print("Soy un",self.tipo)

class automovil(vehiculo):
    def __init__(self):
        self.tipo = "automovil"
        super().__init__()

Lorean = automovil()
Lorean.que_soy()

Current and wanted result:

Automovil creado.
Soy un automovil
Ali Rojas
  • 549
  • 1
  • 3
  • 12
  • 2
    You use a kind-of non-standard terminology, but if I understand your question correctly, you cannot postpone the initialization of a base subobject. The order of [member initialization if fixed](https://stackoverflow.com/q/2669888/580083). You even cannot initialize a base subobject later than a member variable. – Daniel Langr Jun 08 '20 at 07:37
  • @DanielLangr thanks, I needed to know that postponing the initialization of a base class is imposible, so now I can think in others solutions. Sorry for my terminology, my English isn't so good, if you suggest me a more suitable way to ask my question, I will be grateful. – Ali Rojas Jun 08 '20 at 07:59
  • 1
    You're welcome. As for terminology, just look at my previous comment. Instead of _creators_, we have _constructors_ in C++, and _launching_ them basically means _initialization_. – Daniel Langr Jun 08 '20 at 08:39

1 Answers1

1

Simple solution: Don't do output in the constructor. An ideal constructor should do basic initialization of its members, and nothing else. In your simple example, you don't even need to explicitly define (implement) the vehicle constructor, you could use the compiler-generated one.

Another possible solution: Create a (possibly protected) vehicle constructor taking the "type" as an argument and in the car class explicitly "call" that constructor using the constructor initializer-list:

class vehicle
{
protected:
    explicit vehicle(std::string t)
        : type{ t }  // Initialize the type member
    {
        std::cout << type << " created\n";
    }

public:
    void what_am_i()
    {
        std::cout << "I am a " << type << '\n';
    }

private:
    std::string type;
};

class car : public vehicle
{
public:
    car()
        : vehicle{ "car" }  // "Call" the parent constructor for its initialization
    {
    }
};
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks, adding parameters to the parent's creator will work. It is just that the members which will be adjusted in each child are many, and handle it will be uncomfortable. – Ali Rojas Jun 08 '20 at 07:51