为什么在下面的代码中调用Child类的转换器构造函数?C++为什么隐式调用转换器构造函数?
我的意思是,它会通过Child转换器构造函数自动将Base转换为Child。下面的代码编译,但不应该不是编译,因为我还没有提供bool Child::operator!=(Base const&)
?
class Base
{
};
class Child : public Base
{
public:
Child() {}
Child(Base const& base_) :
Base(base_)
{
std::cout <<"should never called!";
}
bool operator!=(Child const&)
{
return true;
}
};
void main()
{
Base base;
Child child;
if(child != base)
std::cout << "not equal";
else
std::cout << "equal";
}
我想这篇文章可能是一个很好的用例,为什么明确的关键字存在。 – sivabudh 2010-03-15 23:44:44
我应该将副本构造函数的标题重命名为转换器构造函数吗? – sivabudh 2010-03-15 23:53:03