开发者

Clearing out Function Hiding compiler warnings in C++

I'm getting "hiding" warnings in my compiler because a class inherits from its parent has the same name but different parameters.

Adding a function that merely pushes out a warning to say that this function does nothing (as is true in the base class, but without the warning) that matches the parameters and name of the base class function to the derived class clears this compiler warning. What are the potential knock-on effects of this on the derived class?

EDIT: Assume that I do not wish them to be able to use the bas开发者_如何学Goe class function. (Don't ask).


Redefining the name in the derived class effectively hides the function in the base class. That's what the warning tells you! :-)

It is a warning, because usually this is a mistake. If it is on purpose, that is ok (but very rare).


The inability of your users to access the base class function through a derived instance without explicitly writing out myDerivedObj.Base::foo(), which they're not likely to do.

Make your function signatures match up, instead, or change the function name.


You need to unhide the base class function in the derived class as:

using Base::Function;

Example:

class Base
{
public:
   void Function(int) { cout << "Function(int)" << endl; }
};

class Derived : public Base
{
public:
   using Base::Function; //NOTE THIS LINE : Unhiding base class function!
   void Function(const char *) { cout << "Function(const char *)" << endl; }
};

Derived d;
d.Function(10); //this calls Base::Function

Demo : http://ideone.com/OTBxg

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜