Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class method and variable with same name, compile error in C++ not in Java?

Tags:

java

c++

People also ask

Can variables and methods have the same name Java?

Yes it's fine, mainly because, syntactically , they're used differently.

Can a function and a variable have the same name C++?

Case matters: alpha, Alpha, and ALPHA are different names. Variables and functions have separate name spaces, which means a variable and a function can have the same name, such as value and value(), and Mata will not confuse them.

Can a variable and class have same name?

10/2 is also relevant: "A class name or enumeration name can be hidden by the name of a variable, data member, function, or enumerator declared in the same scope." Unless the name is hidden, there is no need to the elaborated type specifier.

Can a method have the same name as a field in Java?

Java static code analysis: Methods and field names should not be the same or differ only by capitalization.


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

&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:

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

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. :

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.

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. return the addres of the noobvar function , i.e. 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.


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.


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.

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.


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

13 Overloading

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.