How are member types implemented?

前端 未结 3 1396
灰色年华
灰色年华 2020-12-13 14:41

I\'m looking at this resource:

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

For example, the iterator member type on class vector.

Woul

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 15:27

    The C++ Standard does not use this phrase either. Instead, it would call it a nested type name (§9.9).

    There are four ways to get one:

    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)
    };
    

    Nested type names are defined in class scope, and in order to refer to them from outside that scope, name qualification is required:

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

提交回复
热议问题