mardi 30 août 2016

Is it possible to create an instance of a class within the same class?

Say I have a class:

class Foo{
public:

    Foo(){
    }

    //Is it possible to create a function like this:
    virtual Foo* createOb(){
       //Should create a new Foo,Bar or Fiz, depending on the actual object type.
    }
}

class Bar: public Foo{
public:

    Bar(){
    }
}

class Fiz: public Foo{
public:

    Fiz(){
    }
}

Is it possible to have a method createOb() in the base class, so when createOb() is called on an instance of one of the derived classes, that an instance of the derived class is created ?

Aucun commentaire:

Enregistrer un commentaire