3

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)"?

HBS
  • 243
  • 3
  • 9

2 Answers2

3

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()
barak manos
  • 29,648
  • 10
  • 62
  • 114
  • Thanks for your answer. :-) For class, it seems quite obvious since their dependencies can be easily determined. I changed my question a bit to be more precise and clear and please take a look at it. – HBS Jan 05 '14 at 06:55
  • Why do you consider a pointer of some class/struct/typedef that you define in your code to be a built-in type? – barak manos Jan 05 '14 at 06:58
0

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

Community
  • 1
  • 1
mpark
  • 7,574
  • 2
  • 16
  • 18
  • How about cppreference.com? [static_cast](http://en.cppreference.com/w/cpp/language/static_cast) and [reinterpret_cast](http://en.cppreference.com/w/cpp/language/reinterpret_cast) – Cubbi Jan 05 '14 at 07:11
  • cppreference.com is an excellent source of reference. I chose to use cplusplus.com as a reference for this because cppreference.com follows the standard more closely and therefore does not have a mention of `related types`, whereas cplusplus.com mentions `related types` in the context asked by the OP. – mpark Jan 05 '14 at 07:30