Pure virtual function call interesting cases

Eduard Rostomyan

Consider the following code:

#include <iostream>
using namespace std;

class A
{
  public:
   virtual void f() = 0;
   A(){f();}
};

void A::f() {
    cout<<"A"<<endl;
}

class B:public A{
 public:
    void f(){cout<<"B"<<endl;}
};
int main()
{
 B b;
}

In this case I directly call the virtual function from constructor and get compiler warning which says:
warning: abstract virtual 'virtual void A::f()' called from constructor.
But it executes without termination and prints A.

If I wrap the call of the function like this:

class A
{
  public:
   virtual void f() = 0;
   A(){g();}
   void g(){f();}
};

void A::f(){cout<<"A"<<endl;}

class B:public A{
 public:
    void f(){cout<<"B"<<endl;}
};
int main()
{
 B b;
}

The compiler does not output any warning during compilation but it crushes at runtime with the following message:

pure virtual method called   
terminate called without active exception   
Abort

Can anybody explain the behavior of both of this cases?

Piotr Skotnicki

§ 10.4 Abstract classes [class.abstract] / p6

Member functions can be called from a constructor (or destructor) of an abstract class; the effect of making a virtual call (10.3) to a pure virtual function directly or indirectly for the object being created (or destroyed) from such a constructor (or destructor) is undefined.

In brief: The effect of making a call to a pure virtual function directly or indirectly for the object being created from constructor is undefined.

A call to pure virtual member functions cannot be used from a constructor or a destructor, no matter if the call is direct or indirect, because then you end up with an undefined behavior.

The only useful example of providing the implementation of a pure virtual function is when calling it from a derived class:

struct A
{
    virtual void f() = 0;
};

void A::f()
{
    cout<<"A"<<endl;
}

struct B : A
{
    void f()
    {
        A::f();
        cout<<"B"<<endl;
    }
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Are there cases where using std::set_terminate does not catch a C++ pure virtual function call?

From Dev

Avoiding "Pure Virtual Function Call" in Derived Class C++

From Dev

Suppress Pure Virtual Function call modal dialog and crash silently

From Dev

Suppress Message Box R6025 Pure Virtual Function Call

From Dev

Making SDL call a pure virtual member function as a event callback?

From Dev

Pure virtual function implementation

From Dev

gmock ignore "interesting" function call

From Dev

gmock ignore "interesting" function call

From Dev

Pure virtual function overridding virtual function

From Dev

Pure virtual function overridding virtual function

From Dev

C++: call pure virtual function from member function of same class

From Dev

Deriving implementation of pure virtual function

From Dev

How to implement pure virtual function

From Dev

Purpose of private pure virtual function?

From Dev

Class with implementation of pure virtual function

From Dev

box2d CreateFixture with b2FixtureDef gives pure virtual function call

From Dev

How do I turn a C++ class into managed class and call the pure virtual function inside?

From Dev

Is there a way to call the "deleting destructor" of a pure virtual class?

From Dev

calling a pure virtual function from operator<<

From Dev

Is a pure virtual function actually selected in overload resolution?

From Dev

Pure virtual function with implementation fails in XCode

From Java

Is it valid to override virtual function with pure specifier?

From Dev

const qualifier disappears from pure virtual function

From Dev

"Overloading" pure virtual function with different set of arguments

From Dev

Overriding a pure virtual function from templated classes

From Dev

Pure virtual and override function (c++)

From Dev

Is there a pure virtual function in the C++ Standard Library?

From Dev

Is it valid to override virtual function with pure specifier?

From Dev

"Overloading" pure virtual function with different set of arguments

Related Related

  1. 1

    Are there cases where using std::set_terminate does not catch a C++ pure virtual function call?

  2. 2

    Avoiding "Pure Virtual Function Call" in Derived Class C++

  3. 3

    Suppress Pure Virtual Function call modal dialog and crash silently

  4. 4

    Suppress Message Box R6025 Pure Virtual Function Call

  5. 5

    Making SDL call a pure virtual member function as a event callback?

  6. 6

    Pure virtual function implementation

  7. 7

    gmock ignore "interesting" function call

  8. 8

    gmock ignore "interesting" function call

  9. 9

    Pure virtual function overridding virtual function

  10. 10

    Pure virtual function overridding virtual function

  11. 11

    C++: call pure virtual function from member function of same class

  12. 12

    Deriving implementation of pure virtual function

  13. 13

    How to implement pure virtual function

  14. 14

    Purpose of private pure virtual function?

  15. 15

    Class with implementation of pure virtual function

  16. 16

    box2d CreateFixture with b2FixtureDef gives pure virtual function call

  17. 17

    How do I turn a C++ class into managed class and call the pure virtual function inside?

  18. 18

    Is there a way to call the "deleting destructor" of a pure virtual class?

  19. 19

    calling a pure virtual function from operator<<

  20. 20

    Is a pure virtual function actually selected in overload resolution?

  21. 21

    Pure virtual function with implementation fails in XCode

  22. 22

    Is it valid to override virtual function with pure specifier?

  23. 23

    const qualifier disappears from pure virtual function

  24. 24

    "Overloading" pure virtual function with different set of arguments

  25. 25

    Overriding a pure virtual function from templated classes

  26. 26

    Pure virtual and override function (c++)

  27. 27

    Is there a pure virtual function in the C++ Standard Library?

  28. 28

    Is it valid to override virtual function with pure specifier?

  29. 29

    "Overloading" pure virtual function with different set of arguments

HotTag

Archive