类方法和名称相同的变量,在c++中编译错误而不是Java?

时间:2021-12-31 16:48:44
class Test {

      bool isVal() const {
          return isVal;
      }

  private:

      bool isVal;
};

On Compiling this file it says

在编译这个文件时,它说。

testClass.cpp:9: declaration of `bool Test::isVal'

testClass。cpp:9:“bool Test::isVal”声明

testClass.cpp:3: conflicts with previous declaration `bool Test::isVal()'

testClass。cpp:3:与先前声明‘bool Test::isVal()’的冲突

Although the same would work for java

尽管这对java也适用

class Test {

  private boolean isVal;

  public boolean isVal() {
      return isVal;
  }

}

Not sure why C++ cannot handle this.

不知道为什么c++不能处理这个。

6 个解决方案

#1


58  

Because C++ is not Java. You can take the address of a member:

因为c++不是Java。你可选择会员的地址:

&Test::isVal

So you can't have two members have the same name, except that you can overload member functions. Even if you could disambiguate that by some kind of cast, the next problem would already arise at other places.

所以你不能让两个成员有相同的名字,除非你可以重载成员函数。即使您可以通过某种类型的转换消除歧义,下一个问题也会在其他地方出现。

In C++, a lot of people including me usually call data members specially, like putting a m before their name. This avoids the problem:

在c++中,包括我在内的许多人通常会专门调用数据成员,比如在他们的名字前面加上一个m。这避免了问题:

class Test {
public:
    bool IsVal() const { return mIsVal; }
private:
    bool mIsVal;
};

#2


9  

C++ applies name mangling to function names and global variables. Local variables are not mangled. The problem arises because in C you can access the address of a variable or a function (thus in C++ as well) e.g. :

c++对函数名和全局变量应用名称管理。局部变量没有被破坏。问题出现的原因是,在C语言中,您可以访问变量或函数的地址(因此在c++中也是如此),例如:

struct noob{
    bool noobvar;
    void noobvar(){};
};

One can say, why not apply name mangling to local variables as well and then have an internal local representation such as

人们可以这样说,为什么不将名称修改为局部变量,然后有一个内部的本地表示,比如。

bool __noobvar_avar;
void __noobvar_void_fun;

and suppose that they receive the addresses during execution 0x000A and 0x00C0 respectively.

假设它们分别在执行0x000A和0x00C0期间接收地址。

However if we write somewhere in the code:

但是如果我们在代码中写到:

&noob::noobvar

What should the program do ?

程序应该做什么?

  1. return the address of the variable noobvar , i.e. 0x000A
  2. 返回变量noobvar的地址,即0x000A
  3. return the addres of the noobvar function , i.e. 0x00C0
  4. 返回noobvar函数的addres,即0x00C0

You can see that since in C , and therefore in C++ , you can issue an "address of", it is not legal to have variables and functions with the same name within the same scope of resolution.

您可以看到,由于在C中,因此在c++中,您可以发出一个“address”,在相同的解析范围内拥有名称相同的变量和函数是不合法的。

#3


6  

Functions in c/c++ are just pointers to a location in memory where the code is located, isVal (as a boolean) and isVal (as a function) are therefore ambiguous.

c/c++中的函数只是指向代码所在内存中的位置的指针,因此isVal(作为布尔)和isVal(作为函数)是不明确的。

#4


3  

The quick answer is "because that's the way C++ works." C++ doesn't have a separate name space for member variables and member functions (ie, "methods") where Java (apparently, as I haven't tried this) does.

快速的答案是“因为这就是c++的工作方式。”c++没有一个单独的成员变量和成员函数的名称空间(即“方法”),而Java(显然,我还没有尝试过)则有。

In any case, remember the old story about the guy who went to a doctor and said "Doc, it hurts when I do this." To which the doctor replied "well, don't do that!" This is a language peculiarity on its way to becoming a Dumb Programmer Trick.

无论如何,记住那个老故事,那个人去看医生,说:“医生,我这样做很疼。”医生回答说:“好吧,别那么做!”这是一种正在成为一种愚蠢的程序员技巧的语言特性。

#5


2  

The following section from the C++ Draft Standard N3337 specifies when a name can be overloaded.

下面来自c++草案标准N3337的一节指定何时可以重载一个名称。

13 Overloading

13个重载

1 When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded. By extension, two declarations in the same scope that declare the same name but with different types are called overloaded declarations. Only function and function template declarations can be overloaded; variable and type declarations cannot be overloaded.

当为同一范围内的单个名称指定两个或多个不同的声明时,该名称将被重载。通过扩展,在相同范围内声明相同名称但具有不同类型的两个声明称为重载声明。只能重载函数和函数模板声明;变量和类型声明不能重载。

When you define a class as:

当你定义一个类为:

class Test {

      bool isVal() const {
          return isVal;
      }

  private:

      bool isVal;
};

you are overloading the name isVal within the scope of the class. Such an overload is allowed only when isVal is a member function. It is not allowed when isVal is a member variable.

您正在类范围内重载名称isVal。只有当isVal是成员函数时才允许这种重载。当isVal是成员变量时是不允许的。

#6


-1  

If you have some reason to use same names for variable and method (maybe reduce naming for things have almost same purpose,etc??), I suggest that just naming them with different case:

如果您有一些理由使用相同的变量名和方法(可能减少命名的目的几乎相同,等等),我建议用不同的例子来命名它们:

class Test 
{
    private bool isVal;
    public bool ISVAL() 
    {   return isVal;  }
}

#1


58  

Because C++ is not Java. You can take the address of a member:

因为c++不是Java。你可选择会员的地址:

&Test::isVal

So you can't have two members have the same name, except that you can overload member functions. Even if you could disambiguate that by some kind of cast, the next problem would already arise at other places.

所以你不能让两个成员有相同的名字,除非你可以重载成员函数。即使您可以通过某种类型的转换消除歧义,下一个问题也会在其他地方出现。

In C++, a lot of people including me usually call data members specially, like putting a m before their name. This avoids the problem:

在c++中,包括我在内的许多人通常会专门调用数据成员,比如在他们的名字前面加上一个m。这避免了问题:

class Test {
public:
    bool IsVal() const { return mIsVal; }
private:
    bool mIsVal;
};

#2


9  

C++ applies name mangling to function names and global variables. Local variables are not mangled. The problem arises because in C you can access the address of a variable or a function (thus in C++ as well) e.g. :

c++对函数名和全局变量应用名称管理。局部变量没有被破坏。问题出现的原因是,在C语言中,您可以访问变量或函数的地址(因此在c++中也是如此),例如:

struct noob{
    bool noobvar;
    void noobvar(){};
};

One can say, why not apply name mangling to local variables as well and then have an internal local representation such as

人们可以这样说,为什么不将名称修改为局部变量,然后有一个内部的本地表示,比如。

bool __noobvar_avar;
void __noobvar_void_fun;

and suppose that they receive the addresses during execution 0x000A and 0x00C0 respectively.

假设它们分别在执行0x000A和0x00C0期间接收地址。

However if we write somewhere in the code:

但是如果我们在代码中写到:

&noob::noobvar

What should the program do ?

程序应该做什么?

  1. return the address of the variable noobvar , i.e. 0x000A
  2. 返回变量noobvar的地址,即0x000A
  3. return the addres of the noobvar function , i.e. 0x00C0
  4. 返回noobvar函数的addres,即0x00C0

You can see that since in C , and therefore in C++ , you can issue an "address of", it is not legal to have variables and functions with the same name within the same scope of resolution.

您可以看到,由于在C中,因此在c++中,您可以发出一个“address”,在相同的解析范围内拥有名称相同的变量和函数是不合法的。

#3


6  

Functions in c/c++ are just pointers to a location in memory where the code is located, isVal (as a boolean) and isVal (as a function) are therefore ambiguous.

c/c++中的函数只是指向代码所在内存中的位置的指针,因此isVal(作为布尔)和isVal(作为函数)是不明确的。

#4


3  

The quick answer is "because that's the way C++ works." C++ doesn't have a separate name space for member variables and member functions (ie, "methods") where Java (apparently, as I haven't tried this) does.

快速的答案是“因为这就是c++的工作方式。”c++没有一个单独的成员变量和成员函数的名称空间(即“方法”),而Java(显然,我还没有尝试过)则有。

In any case, remember the old story about the guy who went to a doctor and said "Doc, it hurts when I do this." To which the doctor replied "well, don't do that!" This is a language peculiarity on its way to becoming a Dumb Programmer Trick.

无论如何,记住那个老故事,那个人去看医生,说:“医生,我这样做很疼。”医生回答说:“好吧,别那么做!”这是一种正在成为一种愚蠢的程序员技巧的语言特性。

#5


2  

The following section from the C++ Draft Standard N3337 specifies when a name can be overloaded.

下面来自c++草案标准N3337的一节指定何时可以重载一个名称。

13 Overloading

13个重载

1 When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded. By extension, two declarations in the same scope that declare the same name but with different types are called overloaded declarations. Only function and function template declarations can be overloaded; variable and type declarations cannot be overloaded.

当为同一范围内的单个名称指定两个或多个不同的声明时,该名称将被重载。通过扩展,在相同范围内声明相同名称但具有不同类型的两个声明称为重载声明。只能重载函数和函数模板声明;变量和类型声明不能重载。

When you define a class as:

当你定义一个类为:

class Test {

      bool isVal() const {
          return isVal;
      }

  private:

      bool isVal;
};

you are overloading the name isVal within the scope of the class. Such an overload is allowed only when isVal is a member function. It is not allowed when isVal is a member variable.

您正在类范围内重载名称isVal。只有当isVal是成员函数时才允许这种重载。当isVal是成员变量时是不允许的。

#6


-1  

If you have some reason to use same names for variable and method (maybe reduce naming for things have almost same purpose,etc??), I suggest that just naming them with different case:

如果您有一些理由使用相同的变量名和方法(可能减少命名的目的几乎相同,等等),我建议用不同的例子来命名它们:

class Test 
{
    private bool isVal;
    public bool ISVAL() 
    {   return isVal;  }
}