mercredi 4 novembre 2015

Can I override a virtual function with a pure virtual one?

I have three classes: B, D and G. D is a B and G is a D. Both B and D are abstract. B is from a third party.

B has a non-pure, virtual method that G needs to implement (to be a D). Can I and is it good practice to redefine/override a virtual function to be pure virtual?

Example:

class B // from a third party
{
public:
   virtual void foo();
};

class D : public B
{
public:
   void foo() override = 0; // allowed by gcc 4.8.2
   virtual void bar() = 0;
};

class G : public D
{
public:
   // forgot to reimplement foo
   void bar() override;
};

int main()
{
   G test;  // compiler error is desired
}

To the question of "can I?" gcc allows it, but I do not have the terms/vocabulary to verify the behavior is part of the standard or is undefined and happens to work today.

Aucun commentaire:

Enregistrer un commentaire