I consider (int, double ...) as classes

I am new to C ++ and am a little versed in writing C ++.

According to the “C ++ Primer”, the old style is similar: an int (variable) or (int) variable, while the new ones introduced by the C ++ standard include static_cast <>, const_cast <>, reinterpret_cast <>, and dynamic_cast <>.

  • Is static_cast <> the equivalent of "old style"?

  • I think this is not so, if I consider the basic data types (int, double ...) as a class, then it would be convenient to use only int (object) to perform casting? Does C ++ standardly implement base types as a class?

+3
source share
1 answer

1. :

int i;
double d = 3.14;
i = static_cast<double>(d); //(double)d;
const char* p = reinterpret_cast<char*>(&d); //(char*) &d;
char* q = const_cast<char*>(p); //(char*) p;

2. (, ), .

int i(10); //same as int i = 10

, (static_cast ).

+9

Source: https://habr.com/ru/post/1734756/


All Articles