gpt4 book ai didi

c++ - 变量在 C++ 中如何以及在哪里可能没有关联的名称?

转载 作者:行者123 更新时间:2023-12-01 12:44:02 25 4
gpt4 key购买 nike

在 C++17 标准中声明(强调我的):

"A variable is introduced by the declaration of a reference other than a non-static data member or of an object. The variable’s name, if any, denotes the reference or object."

Source: ISO/IEC 14882:2017 (C++17), §6/6 - "Basic concepts"


为什么是“ 如果有 ”?变量可以在 C++ 中省略名称吗?
如果我查看 cppreference:

"In C++, a variable is actually just a bit of memory that has been reserved for the program’s use. You refer to it using a variable name, so you don’t need to worry about where it is in memory (though you can find out its memory address, and even specify its location, if you want)."

Source: https://en.cppreference.com/book/intro/variables


或者维基百科(我知道它不是最好的来源,但仍然很常见):

"In computer programming, a variable or scalar is a storage address (identified by a memory address) paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value. The variable name is the usual way to reference the stored value, in addition to referring to the variable itself, depending on the context."

Source: https://en.wikipedia.org/wiki/Variable_(computer_science)


这表示变量应始终提供与其关联的名称,无论引用的对象/值是否被它访问。
  • 变量在 C++ 中是否可能没有名称?
  • 如果是,如何以及在哪里(如果可能有多种情况)?

  • 或者,如果我误解了某些内容,那么如何解释“ 如果有 ”?

    有关的:
    What is the difference between a variable, object, and reference?

    最佳答案

    正如评论中提到的,

    void foo(int) {}
    // ^
    定义一个没有名字的变量。
    [basic.pre]/6 :

    A variable is introduced by the declaration of a reference other than a non-static data member or of an object.


    它显然不是一个引用,但它是一个对象吗?是的。
    [intro.object]/1

    An object is created by a definition, [...]


    并且函数定义的参数是定义( [basic.def]/2 )。

    关于c++ - 变量在 C++ 中如何以及在哪里可能没有关联的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62537581/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com