C++ Why is the copy constructor implicitly called?
- by ShaChris23
Why is the Child class's copy constructor called in the code below?
I mean, it automatically converts Base to Child via the Child copy constructor. The code below compiles, but shouldn't it not compile since I haven't provided 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";
}