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

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;
};

Leave a Comment