为什么我应该使用“使用”关键字来访问我的基类方法?
我编写了下面的代码来解释我的问题。如果我注释第 11 行(使用关键字“using”),编译器不会编译该文件并显示此错误:从 'char' 到 'const char*' 的转换无效
。在Son
类中似乎没有看到Parent
类的方法void action(char)
。
为什么编译器会这样?还是我做错了什么?
class Parent
{
public:
virtual void action( const char how ){ this->action( &how ); }
virtual void action( const char * how ) = 0;
};
class Son : public Parent
{
public:
using Parent::action; // Why should i write this line?
void action( const char * how ){ printf( "Action: %c\n", *how ); }
};
int main( int argc, char** argv )
{
Son s = Son();
s.action( 'a' );
return 0;
}
I wrote the below code in order to explain my issue. If I comment the line 11 (with the keyword "using"), the compiler does not compile the file and displays this error: invalid conversion from 'char' to 'const char*'
. It seems to not see the method void action(char)
of the Parent
class in the Son
class.
Why the compiler behave this way? Or have I done something wrong?
class Parent
{
public:
virtual void action( const char how ){ this->action( &how ); }
virtual void action( const char * how ) = 0;
};
class Son : public Parent
{
public:
using Parent::action; // Why should i write this line?
void action( const char * how ){ printf( "Action: %c\n", *how ); }
};
int main( int argc, char** argv )
{
Son s = Son();
s.action( 'a' );
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
派生类中声明的
action
隐藏了基类中声明的action
。如果您在Son
对象上使用action
,编译器将在Son
中声明的方法中搜索,找到一个名为action
的方法>,然后使用它。它不会继续在基类的方法中搜索,因为它已经找到了匹配的名称。然后该方法与调用的参数不匹配,并且您会收到错误。
另请参阅 C++ 常见问题解答,了解有关此主题的更多说明。
The
action
declared in the derived class hides theaction
declared in the base class. If you useaction
on aSon
object the compiler will search in the methods declared inSon
, find one calledaction
, and use that. It won't go on to search in the base class's methods, since it already found a matching name.Then that method doesn't match the parameters of the call and you get an error.
See also the C++ FAQ for more explanations on this topic.
令人惊讶的是,这是标准行为。如果派生类声明的方法与基类定义的方法同名,则派生类的方法将隐藏基类的方法。
请参阅C++ 常见问题解答
Surprisingly this is standard behavior. If a derived class declares a method with the same name as a method defined by the base class, the derived class' method hides the base class' one.
See C++ FAQ
请注意:在这种情况下需要使用“using”是一个危险信号,您的代码可能会让其他开发人员感到困惑(毕竟它使编译器感到困惑!)。您可能应该重命名这两种方法之一,以使其他程序员清楚地区别。
一种可能性:
A note of caution: The need to use a "using" in this situation is a red flag that your code may be confusing for other developers (after all it confused the compiler!). It is likely that you should rename one of the two methods to make the distinction clear to other programmers.
One possibility:
如果在派生类中重新定义了任何重载函数,则基类中的所有重载函数都将被隐藏。
包含这两种功能的一种方法是避免类中的函数重载。或者
您可以按使用方式使用
using
关键字。If in a derived class any over loaded function is redefined then all the overloaded function in the base class is hidden.
One way to include both the functionality is to avoid function overloading in classes. or
You can use
using
keyword, as used.通过使用
using
,基类中声明的名称被引入到派生类的命名空间中。然后,当您在派生类中声明这组函数时,编译器会通过隐式
this
指针的类型将它们与基类中具有相同参数类型的函数区分开来。在重载解析期间,需要类类型转换的参数具有最低优先级,当指向派生类的指针转换为指向基类的指针时,就会发生这种情况。
因此,可以区分具有看似相同的参数列表的两个函数,并且当从派生类型的对象调用它们时,本地声明的函数将是更好的(如果不是精确的)匹配。
我是新手,请指出我的错误。
By using
using
, the names declared in base class are introduced into the namespace of derived class.Then, when you declare the set of functions in the derived class, they are distinguished from those with identical parameter types in its base class by compilers by the type of the implicit
this
pointer.During overload resolution, an argument that needs a class-type conversion, which will happen when a pointer to derived class is converted to a pointer to a base class, is of the lowest priority.
So the two functions with seemingly identical parameter list can be distinguished, and when calling them from an object of the derived type, the one locally declared will be a better (if not exact) match.
I am a newbie and please point out my misunderstanding.