gpt4 book ai didi

c++ - 结构 vector 工程

转载 作者:太空狗 更新时间:2023-10-29 23:42:08 24 4
gpt4 key购买 nike

我的程序写错了

struct vector<int> v;

代替

vector<int> v;

但编译器似乎并不关心:http://codepad.org/TCPb8p2u

为什么有效?使用或不使用 struct 是否存在一些差异?

最佳答案

如果你写class,它也可以。

class vector<int> v;

看这个:http://www.ideone.com/EoJxk

这实际上是旧的 C 风格。 C++ 标准在 §3.4.4 节中将其称为elaborated-type-specifier

关键字struct(或classenum)有时用于消除歧义,或使隐藏名称对编译器可见。考虑标准本身的以下示例(来自第 §9.1/2 节)。请注意,存在一个名为 statstruct 并且还存在一个完全相同的 function:

struct stat {
// ...
};
stat gstat; // use plain stat to define variable

int stat(struct stat*); // redeclare stat as function

void f()
{
struct stat* ps; // struct prefix needed to name struct stat
// ...
stat(ps); //call stat()
// ...
}

§9.1/2 说,

A class definition introduces the class name into the scope where it is defined and hides any class, object, function, or other declaration of that name in an enclosing scope (3.3). If a class name is declared in a scope where an object, function, or enumerator of the same name is also declared, then when both declarations are in scope, the class can be referred to only using an elaborated-type-specifier (3.4.4).

关于c++ - 结构 vector 工程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5749892/

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