Jump to content
  • Advertisement
Sign in to follow this  
Sepiantum

Pure virtual functions

This topic is 2593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Quick question. If I define a pure virtual function inside a C++ class while providing a body for it and later extend that class without redefining that pure virtual function, I can still use the extended class normally right? I know that pure virtual classes can only be created as pointers to an extended class that provides a body for all pure virtual functions, but what if the original class has a body for a pure virtual function? Does it still need to be defined in an extended class?

Share this post


Link to post
Share on other sites
Advertisement
A class that extends it must still provide an implementation of the pure virtual function, regardless of whether one exists in the original class or not.


L. Spiro

Share this post


Link to post
Share on other sites
Hidden
If the original class has a body for the pure virtual function, then it isn't a pure virtual function.

Share this post


Link to post
Hidden

If the original class has a body for the pure virtual function, then it isn't a pure virtual function.

This is misleading.
Being pure only means a class inheriting from it must supply its own implementation of the function. It is still pure even if the base class provides its own.
http://en.wikipedia.org/wiki/Virtual_function#Abstract_classes_and_pure_virtual_functions
Note that it is not common for base classes to provide their own implementations, but doing so does not corrupt the definition of a pure virtual function.


L. Spiro

Share this post


Link to post
I don't see how you drew that conclusion.
Pure virtual destructors are not really common; I can't think of any reason to use one off the top of my head, and I have never used one myself.


L. Spiro

Share this post


Link to post
Share on other sites
I'm just saying that because I'm providing wrapper classes for the Win32 API and because my Window class is only supposed to lay down the foundations, I don't want anybody creating an instance of it. Mainly because in the class, I forward all messages to DefWindowProc(...). The point is for the user to extend the class and provide handling code for various messages.

Share this post


Link to post
Share on other sites
If you want to use pure virtuals to force the user to handle some set of functions, you are doing it the right way.

If you want to use pure virtuals just to make sure the user cannot make an instance of your class directly, you are doing it the wrong way.
If the latter is your goal, use regular virtual functions and make the constructor protected.


L. Spiro

Share this post


Link to post
Share on other sites

If I define a pure virtual function inside a C++ class while providing a body for it...

[s]I'm confused. Right here. Do you mean providing a body for the pure virtual function or a body for the C++ class? Because you can't provide a body for a pure virtual function (that's why it's pure, because it has no body) and not providing a definition for a class is just a forward declaration, in which you couldn't have put a pure virtual function, so you'd have to define the class, making the "while providing a body for it" statement redundant...[/s]
See the post below...

Share this post


Link to post
Share on other sites

[quote name='Sepiantum' timestamp='1320892090' post='4882364']
If I define a pure virtual function inside a C++ class while providing a body for it...

I'm confused. Right here. Do you mean providing a body for the pure virtual function or a body for the C++ class? Because you can't provide a body for a pure virtual function (that's why it's pure, because it has no body) and not providing a definition for a class is just a forward declaration, in which you couldn't have put a pure virtual function, so you'd have to define the class, making the "while providing a body for it" statement redundant...
[/quote]
It is a common mistake to define a pure virtual function as "a function with no body that must be overridden", but in fact it is defined as "a function that must be overridden" only.
It is valid to give them bodies, and they are still pure virtual even with bodies.


L. Spiro

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!