vendredi 6 octobre 2017

Virtual destructor for non pointer member variables

I have a question regarding virtual destructors. I know that if a variable pointer is polymorphic it is neccesary to create a virtual destructor, but if I don't need to do any destruction specific code is it necessary?

For example, the following code:

struct Foo
{
    struct ExtraData
    {
        int myType;
    }
    struct BarData : public ExtraData
    {
        std::string myName;
        float myFoo;
    }
    struct BooData : public ExtraData
    {
        int myBoo;
        double myTime;
    }
    Foo() {}
    ~Foo() { delete myExtradata; }

    int myA;
    int myB;
    ExtraData* myExtraData;
};

myExtraData is created from outside the struct, it can be either via new BarData() or via new BooData(). Would BarData and BooData need a virtual destructor. Or since they don't have member pointer it's okay?

Aucun commentaire:

Enregistrer un commentaire