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

前端 未结 2 1370
迷失自我
迷失自我 2020-12-30 21:29

I have three classes: B, D and G. D is a B and G is a D. Both B and

2条回答
  •  一向
    一向 (楼主)
    2020-12-30 21:55

    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).

提交回复
热议问题