C ++:为什么不能将静态函数声明为const或volatile或const volatile [英] C++ : Why cant static functions be declared as const or volatile or const volatile
问题描述
可能重复:
C ++ - 为什么静态成员函数不能用'const'限定符创建
$想知道静态成员函数不能声明为const或volatile或const volatile的原因吗?
Was curious to know the reason why static member functions cant be declared as const or volatile or const volatile ?
#include<iostream>
class Test
{
static void fun() const
{ // compiler error
return;
}
};
推荐答案
因为这是标准说的:
:静态成员函数没有this指针(9.3.2)。 -end note]
static
成员
函数不应为virtual
。成员函数中不应有code> static 和非 -static
参数类型(13.1)。 静态成员函数不应声明为const
,
volatile
或const volatile
。 (强调我)
2) [ Note: A static member function does not have a this pointer (9.3.2). —end note ] A
static
member function shall not bevirtual
. There shall not be astatic
and a non-static
member function with the same name and the same parameter types (13.1). A static member function shall not be declaredconst
,volatile
, orconst volatile
. (emphasis mine)
这样做的原因是 const
volatile
或 virtual
) (在传统意义上,见下文)。例如,
const
意味着你不能修改对象的成员,但是在静态的情况下,没有对象可以谈论。
The reason for this is that a const
(or volatile
or virtual
) static
method wouldn't make sense (in the traditional sense, see below). For example, const
implies you can't modify the object's members, but in the case of statics, there's no object to talk about.
您可以认为 const
static
可以应用于其他 static
成员,但此选项被视为无意义。
You could argue that a const
static
could apply to other static
members, but this option was regarded as pointless.
这篇关于C ++:为什么不能将静态函数声明为const或volatile或const volatile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!