How does function-style cast syntax work?

2020-02-14 18:18发布

问题:

I guess I am a bit puzzled by the syntax. What does the following mean?

typedef char *PChar;
hopeItWorks = PChar( 0x00ff0000 );

回答1:

It is equivalent to (PChar) 0x00ff0000 or (char *) 0x00ff0000. Syntactically think of it as invoking a one-argument constructor.



回答2:

SomeType(args) means explicit constructor call if SomeType is user-defined type and usual c-cast (SomeType)args if SomeType is fundamental type or a pointer.

PChar is equivalent to char * (pointer). Thus hopeItWorks = (char *)0x00ff0000;



回答3:

typedef char *PChar;

Its typedef of char* to Pchar. Instead of using char* you can define variables with Pchar.

hopeItWorks = PChar( 0x00ff0000 );

Its equivalent to ==>

 hopeItWorks = (char *)( 0x00ff0000 );


标签: c++ casting