gpt4 book ai didi

c++ - 我们可以删除 C++ 中的变量名吗?

转载 作者:太空狗 更新时间:2023-10-29 19:40:50 29 4
gpt4 key购买 nike

我想知道是否有可能,例如我定义了 int temp 然后稍后,我将 temp 定义为 float

我的意思是我想在 .cpp 文件中多次使用名称“temp”。这可能吗?如果可能,怎么做?

编辑:我的意思是在相同的范围内。

最佳答案

不可以,您不能在同一范围 中声明两个具有相同名称的变量。它们的范围必须不同。

像这样:

int temp; // this is global

struct A
{
int temp; // this is member variable, must be accessed through '.' operator
};

int f1()
{
int temp; //local temp, though the global one may by accessed as ::temp
...
}

int f2()
{
int temp; //local

// a new scope starts here
{
int temp; //local, hides the outer temp
...
}

// another new scope, no variable of the previous block is visible here
{
int temp; // another local, hides the outer temp
...
}
}

关于c++ - 我们可以删除 C++ 中的变量名吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8962528/

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