成员类型如何实现? [英] How are member types implemented?

查看:65
本文介绍了成员类型如何实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看此资源:

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

例如,类上的迭代器成员类型向量。

For example, the iterator member type on class vector.

是否将成员类型简单地实现为typedef或向量类中的类似对象?对我来说,成员类型的实际含义还不清楚,我看过几本C ++教科书,他们甚至根本没有提到这个短语。

Would a "member type" simply be implemented as a typedef or something similar in the vector class? It is not clear to me what "member type" actually means, and I've looked at a couple C++ textbooks, they don't even mention this phrase at all.

推荐答案

C ++标准也不使用此短语。取而代之的是,它称其为嵌套类型名称(§9.9)。

The C++ Standard does not use this phrase either. Instead, it would call it a nested type name (§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)
};

嵌套的类型名称是在类范围内定义的,为了从该范围之外引用它们,必须提供姓名资格:

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

这篇关于成员类型如何实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆