成员类型是如何实现的

How are member types implemented?

本文关键字:实现 何实现 成员类 类型 成员      更新时间:2023-10-16

我正在看这个资源:

http://www.cplusplus.com/reference/vector/vector/

例如,vector类上的迭代器成员类型

"成员类型"是否可以在vector类中简单地实现为类型定义或类似的东西?我不清楚"成员类型"到底是什么意思,我看过几本c++教科书,它们甚至根本没有提到这个短语。

c++标准也没有使用这个短语。相反,它会将其称为嵌套类型名称(第9.9节)。

有四种方法可以得到一个:

class C
{
public:
   typedef int int_type;       // as a nested typedef-name
   using float_type = float;   // C++11: typedef-name declared using 'using'
   class inner_type { /*...*/ };   // as a nested class or struct
   enum enum_type { one, two, three };  // nested enum (or 'enum class' in C++11)
};

嵌套类型名称在类范围内定义,为了从该范围外引用它们,需要名称限定:

int_type     a1;          // error, 'int_type' not known
C::int_type  a2;          // OK
C::enum_type a3 = C::one; // OK

Member type简单地表示(该类的)成员类型。它可以是您所说的typedef(在vector的情况下,它可能是T*),也可以是嵌套的class(或struct)。

成员类型可以指'嵌套类'或'嵌套结构'。它意味着类中的类。如果您想参考教科书,那么搜索"嵌套类"。