- [..]
- (4.3) If class B is derived directly or indirectly from class A, conversion of B* to A* is better than conversion of B* to void*, and conversion of A* to void* is better than conversion of B* to void*.
So if I have:
struct A {};
struct M : A {};
struct B : M {};
void f(A*);
void f(void*);
int main()
{
B *bptr = new B();
f(bptr);
}
The call f(bptr)
would prefer the overload f(A*)
over f(void*)
.
But in the second case: conversion of A* to void* is better than conversion of B* to void*. How this conversion can occur? Can you give me an example that triggers this case?
For some reasons I can't finds out a case or an example in which this case is applied. It seems like comparing two unrelated things to each other. But I encounter more in the bullet 4.4.
You can check the whole thing from cppreference para 4:
- If Mid is derived (directly or indirectly) from Base, and Derived is derived (directly or indirectly) from Mid
- a) Derived* to Mid* is better than Derived* to Base*
- b) Derived to Mid& or Mid&& is better than Derived to Base& or Base&&
- c) Base::* to Mid::* is better than Base::* to Derived::*
- d) Derived to Mid is better than Derived to Base
- e) Mid* to Base* is better than Derived* to Base*
- f) Mid to Base& or Base&& is better than Derived to Base& or Base&&
- g) Mid::* to Derived::* is better than Base::* to Derived::*
- h) Mid to Base is better than Derived to Base
void*
overload, it would goB*
->A*
->void*
? This might matter for deep inheritances whereB*
andA*
point to different places. – Cristenvoid f(A*, void*); void f(void*, B*); A *aptr = new A(); f(aptr, bptr);
would choosef(void*, B*)
? – PsychologizeB* -> A* -> void*
isn't possible to begin with. – Strapped