本文介绍了C ++中的类型和名称有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读只是为类型创建新名称的方式。

A type has none, one or several names. Typedef and alias are simply means of creating a new name for a type.

public private 关键字与

为了显式声明特定类型的对象,您需要该类型的名称。 自动不需要此功能。例如,如果您使用未命名的类,则该类没有名称,但仍可以使用auto 。

In order to explicitly declare an object of a particular type you need a name for that type. auto doesn't need this. If you for example use an unnamed class as a return type, this class has no name but auto can still be used on it.

一个类型始终最多具有一个'。即使通过typedef或别名使用它,编译器也以该名称(或实际上是该名称的原始版本)使用它。因此:

An type will always have at most one 'true name'. Even when using it through a typedef or alias, the compiler uses it under this name (or actually the raw version of this name). So:

class A {};
typedef A B;
std::cout << typeid(B).name();

打印 A类。未命名的对象不能使用真实名称。但是,当使用 typedef decltype 时。可以创建一个新名称。如果此名称用于创建对象。 typeid()。name 将打印新分配的名称。如果未取消命名,则将以真名开头。

Prints "class A". An unnamed object cannot be given a 'true name'. However, when using typedef and decltype. A new name can be created. If this name is then used to create objects. typeid().name will print the newly assigned name. If the object wasn't unnamed to start with the 'true name' name will be printed.

情况:


  1. 不同之处在于,首先使用的是私有声明的名称。这是非法的。这与类型推导的不同方式如何工作有关。正如Scott Meyers在所述。由于public函数调用提供了这种类型,因此返回类型为public。但是, Bar 本身不是公开的。差异很小,但这就是原因。




    这只是设计中的决定。这很有意义,有时您只希望在返回结构时使用它。

  1. The difference is that in the first you are using a privately declared name. Which is illegal. This has to do with how the different ways of type deduction work. As Scott Meyers explains here. Since a public function call provides this type, the return type is public. However, Bar on itself is not public. It's a small difference, but that is the reason.

    This simply is a decision that has been made in the design. It makes sense, sometimes you only want a struct to be used when you return it.

这里也一样。没什么区别,但是 Foo :: Bar 根本无法访问。

The same goes here. There is no difference, however Foo::Bar simply is inaccessible.






编辑

如。实际上, int是int类型的名称。但这绝不是唯一的名称 int32_t ,例如,是大多数编译器上相同类型的另一个名称(在其他系统上, int16_t 等效于 int )。

int is a bit special, being a fundamental type. 'int' indeed is the name of the type int. But it's by no means the only name, int32_t, for example is another name for the exact same type on most compilers(on other systems int16_t is equivalent to int).

std::cout << typeid(int32_t).name();

打印 int。

注意:


  • 我不使用别名作为对象其他名称的指示符,

  • I've refrained from using alias as an indicator for other names of an object, since this might cause confusion with the alias keyword.

我从经验中收集了大部分信息。所以我可能会错过一些地方。

Most of this I have gathered from experience. So I might have missed some bits.

由于缺少更好的词,我使用了真实姓名一词。如果有人知道这个词的官方名称或更好的词,我将很高兴听到:)。

By lack for a better word i have used the expression 'true name'. If anybody knows the official or a better word for this i'd be happy to hear it:).

这篇关于C ++中的类型和名称有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:23