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

后端 未结 6 769
别那么骄傲
别那么骄傲 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:30

    Good question.

    I believe conceptually const-ness applies to well-defined object or a data structure. Not to global/static or etc.

    The same way I may ask why a global (or alternatively namespace-specific) function may not be const, i.e. it may promise not to modify any global (or namespace-specific) variables.

    This doesn't make too much sense IMHO. But yes, const-ness of static members belonging to a specific class - this might be useful in some cases IMHO.

提交回复
热议问题