gpt4 book ai didi

c++ - 将 C++ 成员函数声明为静态常量会产生错误

转载 作者:太空狗 更新时间:2023-10-29 21:06:38 24 4
gpt4 key购买 nike

我有以下类接口(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com