我有以下类接口(interface):
class Test
{
public:
Test();
static void fun() const;
private:
int x;
static int i;
};
Test.cpp 包含 fun()
的实现:
void Test::fun() const
{
cout<<"hello";
}
它给我错误... 静态成员函数不允许修饰符
错误是什么意思?我想知道为什么我无法创建既是静态函数又是 const 函数的原因。
最佳答案
void fun() const;
意味着 fun 可以应用于 const 对象(以及非 const)。 如果没有 const 修饰符,它只能应用于非 const 对象。
根据定义,静态函数不需要对象。
关于c++ - 将 C++ 成员函数声明为静态常量会产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7251075/