成员类型如何实施?

joh*_*ers 16 c++

我在看这个资源:

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

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

"成员类型"是否只是在vector类中实现为typedef或类似的东西?我不清楚"成员类型"究竟意味着什么,我看了几本C++教科书,他们根本就没有提到这句话.

jog*_*pan 33

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)
};
Run Code Online (Sandbox Code Playgroud)

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

int_type     a1;          // error, 'int_type' not known
C::int_type  a2;          // OK
C::enum_type a3 = C::one; // OK
Run Code Online (Sandbox Code Playgroud)

  • 我希望本网站上的每一个答案都具有清晰,简洁和高效的特点.谢谢. (5认同)