我编写了下面的代码来解释我的问题。如果我注释掉第11行(使用关键字“using”),编译器将无法编译文件并显示此错误:
invalid conversion from 'char' to '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;
}
Son s = Son();
,这只会创建一个临时对象并调用复制构造函数。只需输入Son s;
即可。 - Charles Salviausing ...
,像新语言(例如 TypeScript)一样使用this->action(...)
来避免错误。 - Top-Master