【发布时间】:2010-12-26 04:37:21
【问题描述】:
我编写了以下代码来解释我的问题。如果我注释第 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;
}
【问题讨论】:
-
请告诉我:如果你删除“const char how”中的 const 会怎样?
-
您无需输入
Son s = Son();。那只是创建一个临时的,然后调用复制构造函数。只需输入Son s; -
为什么C++设计成那个版本:stackoverflow.com/questions/4837399/…
标签: c++ oop inheritance using