For the life of me I cannot find a good explanation of what are the rules that are used to convert a typedef to a C++ statement. The simple cases I understand. But consider this from Danny Kalev:
typedef char * pstr; int mystrcmp(const pstr, const pstr); //wrong!
Danny Kalev then writes:
The sequence const pstr actually means char * const (a const pointer to char); not const char * (a pointer to const char.
I cannot find anywhere the rule to explain why "const pstr" would be converted to "char * const".
Gracias por cualquier ayuda.
preguntado el 22 de mayo de 12 a las 17:05
Eso es porque
pstr es un alias para
char* y cuando lo hagas
const pstr es un poco como decir
const (char*) y no
A typedef isn't like a macro; it doesn't just perform simple text replacement. The typedef defines a single unit, and the additional
const applies to the entire thing. The unit defined is a pointer, so applying
const to it give you a const pointer.
The outcome you expected would require the
const to "reach inside" the
pstr type to apply to something internal. It would get worse the more pointer levels were declared inside that type. Consider
typedef char*** pppstr. To make that a
char const***, la
const would have to be inserted three levels deep dentro de
pppstr type. It's better for the rule to consistently apply
const to the outer level, no matter how complicated the type definition really is.