Should a virtual function have a definition?

Do I need to have a definition for a virtual function?

Consider this sample program below:

#include <iostream> using namespace std; class base { public: void virtual virtualfunc(); }; class derived : public base { public: void virtualfunc() { cout << "vf in derived class\n"; } }; int main() { derived d; return 0; } 

This gives a link error:

In the function base::base() :: undefined, a reference to vtable for base

I have no definition for a virtual function in a base class. Why does this error occur, although I did not explicitly call the virtual function?

The interesting thing I find is that if I do not instantiate the object of the derived class, the communication error no longer exists. Why is this? What has an instance associated with the above link error?

+7
source share
5 answers

The ISO C ++ standard indicates that all virtual class methods that are not purely virtual should be defined.

Link:

C ++ 03 Standard: 10.3 Virtual Functions [class.virtual]

A virtual function declared in a class must be defined or declared pure (10.4) in this class, or both; but diagnostics are not required (3.2).

Thus, either you must make the function pure virtual, or give a definition for it.

If you use gcc, you may get some strange errors if you do not follow this standard specification. gcc faq also documents it:

The C ++ ISO standard states that all virtual class methods that are not purely virtual should be defined, but do not require any diagnostics for violations of this rule [class.virtual]/8 . Based on this assumption, GCC will return only implicitly defined constructors, an assignment operator, a destructor, and a virtual class table in the translation module, which defines its first such method without built-in references.

Therefore, if you cannot define this particular method, the linker may complain about the lack of definitions for explicitly unrelated characters. Unfortunately, to improve this error message, you may need to change the layout, and this cannot always be done.

The solution is to determine that all virtual methods that are not pure are defined. Note that the destructor must be defined even if it is declared pure-virtual [class.dtor]/7 .

+11
source

You need to provide the implementation of a virtual function (with its default behavior) if you do not define the function as "pure virtual".

So your example could be:

 class base { public: void virtual virtualfunc() {} //intentionally do nothing; }; 

or

 class base { public: void virtual virtualfunc()=0; //pure virtual; }; 
+8
source

You either need to provide a definition, or mark it as abstract / pure-life.

 void virtual virtualfunc() = 0; 
+3
source

In response to an error about vtable: the virtual command in this case tells C ++ to create a virtual method table in the base class. Thus, when you use polymorphism, C ++ can replace the virtual methods of the base class with methods from the derived class with the same name at run time. This error tells the user that this replacement is not possible. To fix this error, you need to either implement this method or install it as a pure virtual one, adding "= 0" at the end of the definition.

In response to the changes: the reason you don't get an error when creating an object as a base class is because the base class does not need access to the virtual table. On the other hand, if you are really trying to use this method, you should get an error message because no implementation exists. In other words, even if you can create an object of a base class, it is not a complete class.

+1
source

Yes, you will need a body, but perhaps what you are talking about is called pure virtual functions that do not need to be defined in the base class.

The syntax for defining these parameters is as follows:

 void virtual virtualfunc() = 0; 
0
source

All Articles