Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 512
jtooker Avatar asked Nov 04 '15 16:11

jtooker


People also ask

Can you override a virtual function?

Because virtual functions are called only for objects of class types, you cannot declare global or static functions as virtual . The virtual keyword can be used when declaring overriding functions in a derived class, but it is unnecessary; overrides of virtual functions are always virtual.

Should I use Virtual with override?

Use virtual for the base class function declaration. This is technically necessary. Use override (only) for a derived class' override.

When should we use a virtual vs a pure virtual function?

A virtual function is a member function of base class which can be redefined by derived class. A pure virtual function is a member function of base class whose only declaration is provided in base class and should be defined in derived class otherwise derived class also becomes abstract.

Can we override virtual method in C#?

You cannot override a non-virtual method. Virtual properties behave like virtual methods, except for the differences in declaration and invocation syntax. It is an error to use the virtual modifier on a static property.


2 Answers

You asked:

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

The answer is: Yes, you can. From the C++11 standard:

10.4 Abstract classes

5 [ Note: An abstract class can be derived from a class that is not abstract, and a pure virtual function may override a virtual function which is not pure. —end note ]

like image 173
R Sahu Avatar answered Sep 22 '22 23:09

R Sahu


If you compile the code with a more modern compiler then you'll get the following error messages that explain the problem

prog.cc:23:6: error: variable type 'G' is an abstract class    G test;  // compiler error is desired      ^ prog.cc:10:9: note: unimplemented pure virtual method 'foo' in 'G'    void foo() override = 0; // allowed by gcc 4.8.2         ^ 1 error generated. 

As for the Standard then (10.3 Virtual functions)

11 A virtual function declared in a class shall be defined, or declared pure (10.4) in that class, or both; but no diagnostic is required (3.2).

like image 30
Vlad from Moscow Avatar answered Sep 22 '22 23:09

Vlad from Moscow