Char * conversion rules and aliases

In accordance with strict alias rules:

struct B { virtual ~B() {} };
struct D : public B { };

D d;
char *c = reinterpret_cast<char*>(&d);

A char*for any object of a different type. But now the question is, will it point to the same address & d? What is the guarantee provided by C ++ Standard that it will return the same address?

+5
source share
3 answers

c &d , reinterpret-cast c D*, , . , c ( ) char[sizeof(D)] - char: () (, ofile.write(c, sizeof(D));), ( ), .

@Oli, , , . , .

, reinterpret_cast<char*>(static_cast<B*>(&d)) char[sizeof(B)] .

+6

5.2.10, 7 ++ 2003 :

. , r " T1" " T2" ( T1 T2 T2 , T1), , .

" " " ", "".

+2

( , ):

reinterpret_cast , (, , : f.ex. a , , ...).

, , .

char *c = reinterpret_cast<char*>(&d);

c d.

0

All Articles