mardi 3 avril 2018

Using base class constructor to initialize child class

So I have a base class Base and a few child classes. What I want to do is have a base class constructor that creates a child class based on a string argument, like:

Base:Base(string class_name) {
    switch(class_name) {
        case "something":
            *this = new SomeChildClass(...);
            ...
    }
}

This compiles and runs but the object doesn't have the characteristics of the child class and I don't understand why. Shouldn't this work?

Aucun commentaire:

Enregistrer un commentaire