C++ - 为什么不能使用“const”限定符创建静态成员函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7035356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:23:39  来源:igfitidea点击:

C++ - Why static member function can't be created with 'const' qualifier

c++staticlanguage-lawyerconst-method

提问by prabhakaran

Today I got a problem. I am in the need of a staticmember function, constis not a must but a better. But, I didn't succeed in my efforts. Can anybody say why or how?

今天我遇到了一个问题。我需要一个static成员函数,const不是必须的而是更好的。但是,我的努力没有成功。任何人都可以说为什么或如何?

回答by James McNellis

When you apply the constqualifier to a nonstatic member function, it affects the thispointer. For a const-qualified member function of class C, the thispointer is of type C const*, whereas for a member function that is not const-qualified, the thispointer is of type C*.

const限定符应用于非静态成员函数时,它会影响this指针。对于 class 的 const 限定成员函数Cthis指针的类型为C const*,而对于非 const 限定的成员函数,this指针的类型为C*

A static member function does not have a thispointer (such a function is not called on a particular instance of a class), so const qualification of a static member function doesn't make any sense.

静态成员函数没有this指针(此类函数不会在类的特定实例上调用),因此静态成员函数的 const 限定没有任何意义。

回答by iammilind

I agree with your question, but unfortunately the C++ is designed that way. For example:

我同意你的问题,但不幸的是 C++ 就是这样设计的。例如:

class A {
  int i;         //<--- accessed with 'this'
  static int s;  //<---- accessed without 'this'
public:
  static void foo ()  const // <-- imaginary const
  {}
};

As of today, the constis considered in context of this. In a way, it's narrow. It can be made broader by applying this constbeyond thispointer.
i.e. the "proposed" const, which may also apply to staticfunctions, will restrict the staticmembers from any modification.

截至今天,const被考虑在 的上下文中this。在某种程度上,它是狭窄的。通过将其应用于指针const之外,它可以变得更广泛this
即“建议” const,也可能适用于static功能,将限制static成员进行任何修改。

In the example code, if foo()can be made const, then in that function, A::scannot be modified. I can't see any language side effects, if this rule is added to standard. On the contrary, it's amusing that why such rule doesn't exist!

在示例代码中,如果foo()可以const,则在该函数中,A::s不能被修改。如果将此规则添加到标准中,我看不到任何语言副作用。相反,有趣的是为什么这样的规则不存在!

回答by Don Larynx

Without getting into the details, it's because there may or may not be an object modified by the function, so const is ambiguous to the compiler.

不深入细节,因为可能有也可能没有被函数修改的对象,所以 const 对编译器来说是模棱两可的。

Recall that constkeeps objects constant, but there may or may not be an object here to keep constant.

回想一下,const保持对象不变,但这里可能有也可能没有保持不变的对象。

回答by Raghavendar Reddy

It is unfortunate that C++ doesn't accept it as per design but logically there are few use cases in which it validates well.

不幸的是,C++ 没有按照设计接受它,但从逻辑上讲,很少有用例可以很好地验证它。

A function which is class level valid(static) might not change any static data, may be it will just query data should be const. May be it should be like

类级别的有效(静态)函数可能不会更改任何静态数据,可能只是查询数据应该是常量。可能应该是这样

if(Object)
    MakeThisConstant()
else
    MakeStaticDataConstant() // Only in the scope but static data cannot be constant so may be it should in some scenarios

回答by nihal dixit

A 'const member function' is not allowed to modify the object it is called on, but static member functions are not called on any object. It is used directly by scope resolution operator. Thus having a const static member function makes no sense, hence it is illegal.

'const member function' 不允许修改它被调用的对象,但静态成员函数不能在任何对象上调用。它由范围解析运算符直接使用。因此,拥有一个 const 静态成员函数是没有意义的,因此它是非法的。