Why doesn't g ++ complain when a derived class calls a pure virtual database function?

I have a class Basewith a pure virtual function f(). Another class Derivedcomes from Base. I call f()out Derived. And using g ++, I get an error message from the linker.

[agnel@dooku tmp]$ g++ pure_virtual_function_call.cpp 
/tmp/ccGQLHi4.o: In function `Derived::f()':
pure_virtual_function_call.cpp:(.text._ZN7Derived1fEv[_ZN7Derived1fEv]+0x14): undefined reference to `VirtualBase::f()'
collect2: error: ld returned 1 exit status

It seems to me that the error was caught by the linker. Why did the compiler not report this error? Why leave it in the linker?

Here is the code:

#include <iostream>

using namespace std;

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

class Derived : public VirtualBase {
public:
    void f(){
        VirtualBase::f();
        cout << "Derived\n" ;
    }
};


int main(){
    Derived d;
    d.f();
    return 0;
}
+5
source share
2 answers

Since pure virtual functions can have definitions, and if so, you can call them with little or no syntax VirtualBase::f().

, , .

+12

. , . .

+8

All Articles