C ++中的类型和名称有什么区别? [英] What's the difference between type and name in C++?

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

问题描述

我正在阅读此堆栈溢出问题,然后在该问题的代码中添加了以下构造函数,

I am reading this Stack Overflow question, and I added a constructor to the code from that question as the follwing,

class Foo {
    struct Bar { 
        int i; 
        Bar(int a = 5) :i(a) {}; 
    };

  public:
    Bar Baz() { return Bar(); }
};

int main() {
    Foo f;
    // Foo::Bar b = f.Baz();  // error
    auto b = f.Baz();         // ok
    std::cout <<"b.i="<< b.i<<endl;
    return 0;
}

代码输出 bi = 5 。在该问题中,得出的结论是私有名称不可访问,但类型可访问。那么,类型和名称之间通常有什么区别?

The code outputs b.i=5. In that question it concludes that the name of the private is not accessible but the type is. So what's the difference between type and name, generally?

说我有两种特定的情况。

And say I have two specific scenarios.


  1. 以下两个声明之间有什么区别?为什么我可以从 auto b = f.Baz(); 获得输出 bi = 5

Foo::Bar b = f.Baz();
auto b = f.Baz();


  • 如果我添加 typedef Bar B; Foo 的公共部分中,以下内容有什么区别?

  • If I add typedef Bar B; in the public part of Foo, what is the difference between the following?

     Foo::Bar b = f.Baz();
     Foo::B   b = f.Baz(); 
    


  • 如果情况1和2之间存在差异

    If there a difference between scenario 1 and 2?

    推荐答案


    类型和名称之间有什么区别

    What is the difference between type and name

    一种类型没有名称,一个名称或多个名称。 Typedef 和别名只是为类型创建新名称的方式。

    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.






    编辑


    您能举一个类型没有名字的例子吗?上面评论中未命名的工会就是一个例子吗?

    Can you give an example that type has none names? is the unnamed union in the above comment a example of this?

    As described here I use a lambda function as follows:

    auto f = [] () -> struct {int x, y ; } { return { 99, 101 } ; } ;
    

    如果不使用auto或decltype,将无法创建变量 f 。由于类型没有名称。没有名称的类型的另一个例子。

    Without using auto or decltype there would be no way of creating variable f. Since it's type has no name. Another example of a type without a name.

    struct foo
    {
        struct{
            int x;
            int y;
        } memberVar;
    };
    

    将允许您执行以下操作:

    Would allow you to do the following:

    foo bar;
    
    auto baz = bar.memberVar;
    
    std::cout << baz.x;
    

    这当然会产生很多初始化的东西,但是你会明白的:)。 memberVar 的类型未命名。使得无法明确定义 baz

    Which results in a bunch of initialized stuff of course, but you get the idea:). The type of memberVar here is unnamed. Making it impossible to define baz explicitly.


    并且int被认为是int类型的名称吗?

    And is int considered to be a name for type of int?

    int 有点特殊,它是基本类型。实际上, 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 ++中的类型和名称有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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