Implicit casting - which cast is used

Suppose I have a base and child class and a multiple inheritance bit:

class Child : public Base, public AnotherBase { }; 

and functions foo(Base* b) . I also created an instance of Child* c . Then I call foo(c) .

The compiler does implicit listing. But is C style needed, a static_cast<Base*> or something else?

+7
c ++
source share
1 answer

static_cast , and C-style are programmerโ€™s methods that explicitly request type conversion. Your example is a standard implicit conversion, which is described separately, and not in terms of explicit conversions.

Your example is known as derivative conversion and is defined in the standard [conv.ptr]/2 :

N3337: A value of type "pointer to cv D ", where D is a class type, can be converted to prvalue of type "pointer to cv B ", where B is the base class of D If B is an unreachable or ambiguous base class D , a program that requires this conversion is poorly formed. The result of the conversion is a pointer to a subobject of the base class of the derived class object. The null pointer value is converted to the null pointer value for the destination type.

In other words, a D* always implicitly converted to B* with the same const and volitile .

+5
source share

All Articles