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

前端 未结 6 2109
执念已碎
执念已碎 2020-11-30 05:52
class Test {

      bool isVal() const {
          return isVal;
      }

  private:

      bool isVal;
};

On Compiling this file it says

6条回答
  •  暖寄归人
    2020-11-30 06:02

    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.

提交回复
热议问题