Why is the child class constructor constructor called in the code below?
I mean, it automatically converts Base to Child through the constructor of the Child converter. The code below compiles, but shouldn't it compile since I did not provide 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";
}
source
share