mercredi 15 juin 2022

Is a default constructor called on a class member variable if I am explicitly constructing it in the class constructor in c++?

So if I have something like the following:

class MyClass{
    public:
        MyClass(){
            // do other stuff
            *oc = OtherClass(params);
        }
    private:
        OtherClass* oc;
}

When is a constructor called on OtherClass? Would its default be called once as soon as the MyClass initialization begins, and then be redefined with its value constructor during the MyClass constructor? Or does it just not exist during "//do other stuff". What if no default constructor is provided for other class, only a value? Would it be good practice to construct it where it is defined as a member variable?

Aucun commentaire:

Enregistrer un commentaire