Skip to main content
gamedev.net
🔒 Locked

Pure virtual functions

Started by Sepiantum Nov 10, 2011 at 2:28 AM 8 replies 1.2k views
Original Post
Sepiantum
Sepiantum
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?
Follow and support my game engine (still in very basic development)? Link
L. Spiro
L. Spiro
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
I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid
Sepiantum
Sepiantum
So I should make a pure virtual destructor and go from there?
Follow and support my game engine (still in very basic development)? Link
L. Spiro
L. Spiro
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
I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid
Sepiantum
Sepiantum
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.
Follow and support my game engine (still in very basic development)? Link
L. Spiro
L. Spiro
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
I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid
Cornstalks
Cornstalks

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...
L. Spiro
L. Spiro

[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
I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid
Cornstalks
Cornstalks

[quote name='Cornstalks' timestamp='1320898456' post='4882405']
[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
[/quote]

Wow, learn something new every day. Thanks for that.
Sepiantum
Sepiantum
Protected constructors go! TYVM
Follow and support my game engine (still in very basic development)? Link

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.