🔒 Locked
Pure virtual functions
Advertisement
Original Post
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?
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.
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
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
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...
[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 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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement