+ 1

Empty virtual func vs pure virtual func

There is any incovenient in using empty virtual func bodies instead of pure virtual func? The advantaje I see is can create objects of empty ones, working with the other functions and data of the class.. Any observation? Thx

9th Jul 2019, 4:49 AM
BinaryEden
BinaryEden - avatar
1 Odpowiedź
+ 2
Although you can make instances of classes with non pure virtual, virtual functions, it isn’t really meant to be better in any way because of that. Pure virtual just means the function MUST have a definition somewhere else in a derived class. A virtual function that isn’t pure and that has an empty body essentially means it CAN be overridden in a derived class, but if it is not, the current function body will be used which you’ve stated is empty so nothing happens. You cannot create an object of a class with a pure virtual function in it because it has no code to match the function name to without a derived class. Non pure virtual does have code to match, so it will be fine. You should use virtual and pure virtual not for efficiency sake, but instead to force a class to override or just make it possible to do so. Hope this helps!
14th Jul 2019, 11:30 PM
nk361
nk361 - avatar