Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a C++ pure virtual class need a definition? [duplicate]

Tags:

c++

class

Consider:

// in header.h
class A {
public:
    virtual ~A() = 0;
};


class B : public A {
public:
    ~B() override {}
}

The linker reports that it cannot resolve:

external symbol "public: virtual __thiscall A::~A(void)" referenced in function "public: virtual __thiscall B::~B(void)"

I find that I have to write the definition of A::~A().

I used to think that a pure virtual class defines interface (function declaration), and they don't have to contain a function definition. Should I write definitions for all virtual functions in a pure virtual base class? Or should I just need to write the destructor function?

like image 669
Marvis Lu Avatar asked Apr 20 '17 12:04

Marvis Lu


People also ask

Do pure virtual functions need to be defined?

A pure virtual function is a function that must be overridden in a derived class and need not be defined. A virtual function is declared to be “pure” using the curious =0 syntax.

What is the proper way to declare pure virtual?

You declare a pure virtual function by using a pure specifier ( = 0 ) in the declaration of a virtual member function in the class declaration.

Can we define pure virtual function in base 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. Classes having virtual functions are not abstract. Base class containing pure virtual function becomes abstract.

Does a pure virtual class need a destructor?

While defining (providing an implementation) pure virtual methods is rarely useful, you must define a pure virtual destructor. This is because the destructor of a base class is always called when a derived object is destroyed.


2 Answers

It's because unlike regular virtual functions, a destructor isn't just overridden. When you call a regular virtual function on a base class, you end up only calling the function which has overridden that function.

In the case of the destructor, however, the destructors of the base classes must also be called. But since you don't provide an implementation of ~A(), your code fails to link.

But you can define the function even though it's pure virtual: like here.

like image 150
Fatih BAKIR Avatar answered Oct 12 '22 00:10

Fatih BAKIR


Should a C++ pure virtual class need a definition?

The first mistake is that you invent terms on behalf of yourself. There is no such thing as "a pure virtual class". There is only "virtual function" and "pure virtual function". Understanding that there is no such thing "pure virtual class" is a key of understanding why this question does not hold.

I used to think that pure virtual class defines an interface (function declaration)

I think C++ is not your first language, but a second language after Java/C#, and you think in Java/C# ideas that has nothing to do with C++.

C++ has interfaces - it's just the declaration of the class:

struct A{

    void doNothing();

};

//A.cpp:
void A::doNothing(){

}

This is the interface of struct A. Does it have anything to do with inheritance, virtual functions or polymorphism? No. It's just the declaration of the class, what method and properties exist within it.

Every class needs a valid destructor to allow the program to clean the object resources - memory, etc. It has nothing to do with polymorphism. In your example, A needs to be destructed somehow. It doesn't matter that B inherits from it. It must tell the program how to deal with it when it gets out of scope.

As mentioned in the comments, if you only want a virtual destructor (so not UB will be manifested in the case of A* a = new B()), just declare the destructor as default:

virtual ~A() = default;
like image 39
David Haim Avatar answered Oct 12 '22 01:10

David Haim