mardi 27 octobre 2015

Can I create singleton child class?

I have parent class:

class ParentClass {
public:
    ~ParentClass(){}
    static ParentClass &getInstance() {
        static ParentClass instance;
        return instance;
    }

    void methodA() {
        //do some stuff
    }

    void methodB() {
        //do some stuff
        methodA();
        //do some stuff
    }

private:
    ParentClass(){}
}

I want to create child class:

class ChildClass : public ParentClass {
public:
    ChildClass(){}
    ~ChildClass(){}

    methodA() {
        //do some stuff
    }
}

This code have some obvious problems. First of all I can't create ChildClass in this way (parent's constructor is private). And seems ChildClass can't directly inherited from ParentClass. This classes very similar. And I do not want create two similar copy of this classes.

Probably I can create one base class for ParentClass and ChildClass. I'm not sure but does some method exist for creating childs from singleton?

Aucun commentaire:

Enregistrer un commentaire