I have a base class with a virtual function.
virtual CString& Foo();
I want to overload this in subclass like so
CString Foo();
is there a way to hide the base classes virtual function? Something like the new keyword in vb.net or C#
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Why anyone would do something like that? It breaks base class contract.
If you don’t want to implement subclass that has the same interface as base class, why do you inherit at all?
Use composition.
There is no equivalent of C# new keyword in C++.
So you cannot cancel method’s ‘virtualness’.
If you really want to do this you can always:
override a method in subclass as private.
create overload. But the overload has to have different parameters.
But if you do this, IMHO something is wrong with your design.
I wish each C++ compiler caught both of this situations at least as warnings.