Defining a pure virtual function outside a derived class

I have an abstract base class and a derived class in the header file. Is it possible to have a pure virtual function definition outside a derived class?

For instance:

//file .h class Baseclass { public: virtual int vfunc() = o; //assume Ctor and Dctor }; class Derivedclass : public Baseclass { public: int vfunc(); //assume Ctor and Dctor }; 

Now in the cpp file:

 #include <file.h> int Derivedclass :: vfunc() { // Body of the function } 

Is the above method correct / possible?

+4
source share
3 answers

It is not only possible, it is standard practice. The only time you have to worry about putting function definitions in the header is through templates.

+3
source

Yes it is possible. You can define them outside your class if that is what you want to ask.

+1
source

Is it possible to have a pure virtual function definition outside a derived class?

A function is not pure virtual in a derived class; it is only pure virtual in a base class.

The derived class overrides the function and does not have a pure-specifier (bit =0 after the function declaration), therefore DerivedClass::vfunc() not purely virtual and therefore must have a definition somewhere, if used in the program, Defining it in a separate the file from the header is perfectly fine.

0
source

Source: https://habr.com/ru/post/1440583/


All Articles