Why there is no concept of “const-correctness” for class's static member functions?

后端 未结 6 763
别那么骄傲
别那么骄傲 2020-12-19 01:13

Use case:

class A {
  static int s_common;
public:
  static int getCommon () const { s_common; };
};

Typically this results in an error as:

6条回答
  •  醉话见心
    2020-12-19 01:56

    cv-qualifiers affect the function's signature. So you could have:

    class A {
      static int s_common;
    public:
      static void getCommon () const {  };
      static void getCommon () {  };
    };
    

    Now... how would you call the const one? There's no const object to call it on (well, you could call it on a const object, but that's not the point).

    I'm just guessing here, there probably are other reasons. :)

提交回复
热议问题