HBS
HBS

Reputation: 253

Type conversion in C++: related and unrelated types

I was reading "The C++ Programming Language" by Bjarne Stroustrup to learn C++ and encountered with the following passage:

The static_cast operator converts between related types such as one pointer type to another in the same class hierarchy, an integral type to an enumeration, or a floating-point type to an integral type. The reinterpret_case handles conversions between unrelated types such as an integer to a pointer or a pointer to an unrelated pointer type.

To me, it is not clear what determines any two given types be related or unrelated and the examples mentioned seem not that exhaustive.

Stroustrup says that types are classified into 1. Arithmetic types 2. User-defined types and 3. Built-in types, and from the examples above, he regards arithmetic type (int) to be related to a user-defined type (enum). A float type to an int is obvious because they both are arithmetic types.

However, he classifies two pointers, which should be both built-in types according to his definition, to be unrelated.

So what do we exactly mean by saying "two types are related (unrelated)"?

Upvotes: 3

Views: 2191

Answers (2)

mpark
mpark

Reputation: 7904

Two types are related if they are part of the same class hierarchy via means of inheritance.

The static_cast operator converts between related types such as one pointer type to another in the same class hierarchy, an integral type to an enumeration, or a floating-point type to an integral type. The reinterpret_case handles conversions between unrelated types such as an integer to a pointer or a pointer to an unrelated pointer type.

This text seems slightly misquoted since it seems to suggest that (integral types and enumerated types) as well as (floating-point type and an integral type) are related. I think what the text meant to say is something along the lines of:

The static_cast operator converts between pointers to related types (upcast as well as downcast without run-time type-checking), an integral type to an enumeration, ... .

The reinterpret_cast operator converts any pointer type to another, even if they are unrelated, integer to a pointer or ... .

Reference: The wording for static_cast and reinterpret_cast seem to be clearer on cplusplus.com

Upvotes: 0

barak manos
barak manos

Reputation: 30136

In the example below, types A and B are related, while types A and C, as well as B and C, are unrelated:

class A
{
    ...
};

class B : public A
{
    ...
};

class C
{
    ...
};

And in general, I think that types X and Y are related if and only if one of the following conditions holds:

  • X inherits from Y
  • Y inherits from X
  • X has a constructor which takes a Y object by reference, meaning, X::X(Y& y)
  • Y has a constructor which takes a X object by reference, meaning, Y::Y(X& x)
  • X has a casting operator to Y, meaning, X::operator Y()
  • Y has a casting operator to X, meaning, Y::operator X()

Upvotes: 4

Related Questions