c++ - C++ 中的名称和类型

标签 c++ types declaration names

<分区>

我的帖子以我感兴趣的问题开始:

Q1:声明在表示类型的翻译单元中引入一个或多个名称是否属实?

Q2:(如果Q1的答案是肯定的)编译器如何确定声明引入的名称类型?考虑以下声明:

当我开始阅读 c++ working draft 的 3rd clause 时出现了这个问题。

An entity is a value, object, reference, function, enumerator, type, class member, template, template specialization, namespace, parameter pack, or this.

A name is a use of an identifier (2.11), operator-function-id (13.5), literal-operator-id (13.5.8), conversion function-id (12.3.2), or template-id (14.2) that denotes an entity or label (6.6.4, 6.1).

List<Observer *> *_observers;

此声明将 _observers 引入了当前范围。我想了解编译器如何确定 _observers 的类型?我对 c++ 规范中描述的正式算法很感兴趣。

最佳答案

声明可以引入类型、函数、变量。

class A;  // This declares a type, A
A* aPtr;  // This declares a variable, aPtr
          // The type of aPtr is A*
A foo();  // This declares a function, foo.

在你的情况下,

List<Observer*> *_Observers;  // Declares a variable, _observers.
                              // The type of _observers is List<Observer*>*.
                              // For this to be a valid declarion, the types List,
                              // a class template, and Observer must be known 
                              // (declared or defined) before the
                              // variable declaration.

关于c++ - C++ 中的名称和类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23440407/

相关文章:

c++ - 定义与声明类

c++ - C++ 中 SFML 库的编译问题

jquery - Cufon 嵌套悬停问题

c - 为什么枚举数据类型总是在 c 中的 main() 之外声明?

c - 什么是斐波那契偶数项之和(<400 万)? 【大值数据类型混淆】

java - 在 Java SE6 中使用 <?> 进行模板化有什么作用?

c++ - 在 C++ 中的头文件中声明全局结构

c++ - 传递非泛型函数的最有效方法是什么?

C++ 链接器错误 undefined reference to a class definition and its member functions on Linux

c++ - 成员函数什么时候应该有 const 限定符,什么时候不应该?