Used to convert a value of one type to a value of another. Can be used as a replacement for
old style cast operators.
int main()
{
int i;
float f;
i = (float) f * 6;
// old style cast to float
// - does cast refer to (f * 6) or just to f?
i = float (f * 6);
// new style cast to float
// - clear what is being converted
return 0;
}
Type conversions of this form can only be used when the type has a simple single name, that
is
str = char * (ptr);
is not legal, but if a new type name is created with typedef, then the conversion will be
legal C++.
typedef char *string;
str = string(ptr);
old style cast operators.
int main()
{
int i;
float f;
i = (float) f * 6;
// old style cast to float
// - does cast refer to (f * 6) or just to f?
i = float (f * 6);
// new style cast to float
// - clear what is being converted
return 0;
}
Type conversions of this form can only be used when the type has a simple single name, that
is
str = char * (ptr);
is not legal, but if a new type name is created with typedef, then the conversion will be
legal C++.
typedef char *string;
str = string(ptr);
No comments:
Post a Comment