什么是 C++ 中的标量对象?
据我所知,基本类型是标量,数组是聚合的,但用户定义的类型呢?我会根据什么标准将它们分为两类?
As far as I understand it fundamental types are Scalar and Arrays are aggregate but what about user defined types? By what criteria would I divide them into the two categories?
struct S { int i; int j };
class C { public: S s1_; S s2_ };
std::vector<int> V;
std::vector<int> *pV = &v;
推荐答案
简短版本: C++ 中的类型是:
Short version: Types in C++ are:
对象类型:标量、数组、类、联合
Object types: scalars, arrays, classes, unions
引用类型
函数类型
(成员类型)[见下文]
(Member types) [see below]
void
长版
对象类型
Object types
标量
算术(整数、浮点数)
arithmetic (integral, float)
指针:T *
用于任何类型 T
pointers: T *
for any type T
枚举
成员指针
nullptr_t
数组:T[]
或 T[N]
用于任何完整的非引用类型 T
Arrays: T[]
or T[N]
for any complete, non-reference type T
类:class Foo
或 struct Bar
琐碎的课程
Trivial classes
聚合
POD 类
(等等等等)
联合:联合邮编
引用类型:T &
、T &&
用于任何对象或自由函数类型T
References types: T &
, T &&
for any object or free-function type T
函数类型
自由函数:
R foo(Arg1, Arg2, ...)
成员函数:R T::foo(Arg1, Arg2, ...)
void
成员类型是这样工作的.成员类型的形式为 T::U
,但您不能拥有成员类型的对象或变量.您只能拥有成员指针.成员指针具有类型 T::* U
,如果 U
是(自由)对象类型,则它是指向成员对象的指针,并且指针-如果 U
是(自由)函数类型,则为成员函数.
Member types work like this. A member type is of the form T::U
, but you can't have objects or variables of member type. You can only have member pointers. A member pointer has type T::* U
, and it is a pointer-to-member-object if U
is a (free) object type, and a pointer-to-member-function if U
is a (free) function type.
所有类型都是完整的,除了 void
、未定义大小的数组和声明但未定义的类和联合.除了 void
之外的所有不完整类型都可以完成.
All types are complete except void
, unsized arrays and declared-but-not-defined classes and unions. All incomplete types except void
can be completed.
所有类型都可以const
/volatile
限定.
All types can be const
/volatile
qualified.
标头提供了 trait 类来检查这些类型特征中的每一个.
The <type_traits>
header provides trait classes to check for each of these type characteristics.
相关文章