Why doesn't typecasting operator work with inherited class?

I have two classes:

struct B {};
struct D {
  operator B& ();
};

When will I do it;

b = d; // B b; ... D d;

The result corresponds to the expectation in which D::operator B&()( Demo ) is called.

If the parameter is Dchanged to,

struct D : B {
  operator B& ();
};

it is D::operator B&()not called ( Demo ). B::B(const B&)finds a better candidate in D, then D::operator B&()?

+5
source share
1 answer

If Dcomes from B, there is an implicit automatic conversion from Dto B. This takes precedence over a custom conversion operator.

+7

All Articles