C++ 类型系统

595 阅读1分钟

C++中每个变量,函数返回值和函数参数都要有类型,表达式也必须在编译阶段确定类型。就连用户通过class和struct自定义的类型,都需要在执行前确定内存占用大小。

C++ 数据类型分为3种

  1. scalar data,标量类型,分为数据和字符串。
  2. 非 scalar data,就是class或者struct的instance。
  3. POD plain old object,是为了兼容c编译器产生的。

C++是一种强语言类型,每个变量都有type,而且不能被改变。讲一个变量的值赋值给另一个类型的变量,就做类型转化,这种操作有时候是必须的,但是容易产生错误。

基础类型 int double bool char wchar_t, unsigned char, unsigned int, long long

用户自定义类型 class struct enum union

指针类型

int* pNumber;       // Declare a pointer-to-int variable.
*pNumber = 10;      // error. Although this may compile, it is
                    // a serious error. We are dereferencing an
                    // uninitialized pointer variable with no
                    // allocated memory to point to.