Re: reinterpret_cast



"anton bassov" <xxx@xxxxxxx> wrote in message
news:c60bc5d9f40f444ba6a7a0cb83b1decc@xxxxxxxxxxxxxx
Hi Carl

Look at the statment below:

///////////
That's incorrect. struct/class makes no difference: the C-style cast
still
behaves like static_cast, not reinterpret_cast, regardless of whether the
types were declared using the class keyword or the struct keyword. In
fact,

in such cases the C cast is always equivalent to static_cast (which will
produce the same result as reinterpret_cast in some cases).
/////////

This is wrong, believe me

That's not really a good way to defend your point. In this case, a quotation
from the C++ standard would be much more convincing.

For example:

[begin quote "5.4 Explicit type conversion (cast notation)"]

The conversions performed by

--a const_cast (5.2.11),

--a static_cast (5.2.9),

--a static_cast followed by a const_cast,

--a reinterpret_cast (5.2.10), or

--a reinterpret_cast followed by a const_cast,

can be performed using the cast notation of explicit type conversion. The
same semantic restrictions and behaviors apply. If a conversion can be
interpreted in more than one of the ways listed above, the interpretation
that appears first in the list is used, even if a cast resulting from that
interpretation is ill-formed.

[...]

In addition to those conversions, the following static_cast and
reinterpret_cast operations (optionally followed by a const_cast operation)
may be performed using the cast notation of explicit type conversion, even
if the base class type is not accessible:

--a pointer to an object of derived class type or an lvalue of derived class
type may be explicitly converted to a pointer or reference to an unambiguous
base class type, respectively;

[end quote]

The quotation above, however, means that Carl is right and you are wrong.

- the only reason why I mentioned it is because I
actually tried to change struct to class in the example that you have
presented. When I compiled it with struct, everything went the way you
have said. However, when I changed it to class, I got the compile error,
telling me that "static cast exists, but is unavailable".

This is only because that when you write

struct D: A, B {}

it really means

struct D: public A, public B {}

If you change this to

struct D: private A, private B {}

you will have exactly the same error message, so the keyword 'class' is not
relevant at all.

There was no problem with reinterpret_cast.
Therefore, I removed static_cast, and tried to run the program - C-style
cast and reinterpret_cast worked the same way, and both returned wrong
results(they both returned a pointer to class A, rather than to class B)

This means your compiler is buggy. If you had a definition like

class D: A, B {};

D d, *p = &d;

B *b = (B*)p;

the explicit cast notation should give you a valid pointer to the B base,
even though it is not accessible, as remarked in the quotation above. A
compiler giving you a pointer to something else is broken.

S



.



Relevant Pages

  • Re: C variable retyping
    ... whose type is given by the cast. ... >an integer, no conversion is done, and c is used, within the ... In other words, you may take a pointer value, then use a cast to ... which is probably why the C99 folks decided to allow it ...
    (comp.lang.c)
  • Re: two meanings of a cast
    ... as a "let's pretend" cast. ... casts can convert a pointer of one type into a pointer ... There's another such conversion on line four, ... cast converts the value `' to a different type. ...
    (comp.lang.c)
  • Re: integer to pinter conversion
    ... While an integer may be converted to a pointer, ... you don't even need the cast in the code above. ... Since the return type is void *, so if the function funcreturns ... conversion is still being done. ...
    (comp.lang.c)
  • Re: "C vs java"
    ... I wish to hear more on this; what can't you cast in C? ... struct types by copying member values. ... If actual conversion of data is involved, ... Pointer conversions *typically* work by reinterpreting the ...
    (comp.lang.c)
  • Re: Why is this code valid C++?
    ... > null pointer), the evaluation stops. ... No, it does not, this conversion is well defined. ... "Types bool, char, wchar_t, and the signed ... And 5.4 Explicit type conversion (cast notation) makes it quite clear ...
    (comp.lang.cpp)