我们可以在C++中删除变量的名称吗?

Mil*_*d R 3 c++

我想知道是否有可能,例如我int temp后来定义,我定义tempfloat.

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

编辑:我的意思是在同一范围内.

060*_*002 11

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

像这样:

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
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)