声明 C++作为 static const 的成员函数会产生错误

发布于 2024-12-02 08:20:03 字数 360 浏览 0 评论 0原文

我有以下类接口:

class Test
{
public: 
    Test();
    static void fun() const;

private:
    int x;
    static int i;
};

Test.cpp 包含 fun() 的实现:

void Test::fun() const
{
   cout<<"hello";
}

它给了我错误... 静态成员函数上不允许修饰符

什么是错误是什么意思?我想知道为什么我无法创建静态函数和 const 函数。

I have the following class interface:

class Test
{
public: 
    Test();
    static void fun() const;

private:
    int x;
    static int i;
};

Test.cpp contains fun()'s implementation:

void Test::fun() const
{
   cout<<"hello";
}

it is giving me errors... modifiers not allowed on static member functions

What does the error mean? I want to know the reason why I am not able to create a function which is static as well as const.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

中二柚 2024-12-09 08:20:03
void fun() const;

意味着 fun 可以应用于 const 对象(以及非 const)。
如果没有 const 修饰符,它只能应用于非 const 对象。

根据定义,静态函数不需要对象。

void fun() const;

means that fun can be applied to const objects (as well as non const).
Without the const modifier, it can only be applied on non const object.

Static functions by definition need no object.

相对绾红妆 2024-12-09 08:20:03

静态函数在没有实例的情况下工作,而 const 保证函数不会更改实例(即使它需要实例)。

如果您看到翻译后的代码,可能会更容易理解:

   static void fun();

at the end of the day 被翻译为一个不带参数的函数,即

   void fun();

对于另一个例子,

   void fun() const;

at the end of the day 被翻译为

   fun(const Test& self)

以下 形式的函数: ,static void fun() const有两个相互矛盾的含义。

顺便说一句:这种转换适用于所有成员函数(const 或非 const)

Static functions work without an instance, whereas const guarantees that the function will not change the instance (even though it requires an instance).

It may be easier to understand if you see the translated code:

   static void fun();

at the end of the day is translated to a function that takes no argument, namely

   void fun();

For the other example,

   void fun() const;

at the end of the day is translated to a function of the form

   fun(const Test& self)

Thus, static void fun() const has two contradictory meanings.

BTW: This translation occurs for all member functions (const or not)

空宴 2024-12-09 08:20:03

成员函数为 const 意味着无法调用类实例的其他非 const 成员

自由函数不是成员函数,因此它不与类或类实例关联,因此它不能是 const,因为没有成员

静态函数是一种自由函数,其名称范围位于类名称内,使其始终与类型相关,但不与该类型的实例关联,因此仍然没有可以访问的成员。

在最后两种情况下,拥有 const 访问权限是没有意义的,因为没有可以访问的成员。

A member function being const means that the other non-const members of the class instance can't be called.

A free function isn't a member function, so it's not associated as to a class or class instance, so it can't be const as there is no member.

A static function is a free function that have it's name scoped inside a class name, making it always relative to a type, but not associated to an instance of that type, so there is still no member to get access to.

In those two last cases, there is no point in having const access, as there is no member to access to.

幸福丶如此 2024-12-09 08:20:03

我几个小时前在这里回答了这个问题:为什么我们需要将 const 放在函数头末尾,但首先是 static?

(系统对我的回复不满意。自动转换为注释)

i answered this a few hours ago here: Why we need to put const at end of function header but static at first?

(SO system is not happy with my response. automatically converted to comment)

半山落雨半山空 2024-12-09 08:20:03

也许有一个简单的代码示例会有所帮助。

class Foo {
public:
    static void static_function();
    void const_function() const;
};

// Use of static function:
Foo::static_function();

// Use of const function:
Foo f;
f.const_function();

两者之间的主要区别在于 const 函数是一个成员函数——也就是说,它是在 Foo 类的实例上调用的。这意味着您首先需要实例化 Foo 类型的对象,然后该对象充当对 const_function 调用的接收者。 const 本身意味着您不会修改作为该函数调用的接收者的对象的状态。

另一方面,静态函数本质上是一个自由函数,您可以在没有接收对象的情况下调用它。然而,在定义它的类的范围之外,您需要使用类名来限定它:Foo::static_function

这就是为什么拥有一个同时是 staticconst 的函数是没有意义的,因为它们在完全不同的上下文中使用。调用静态函数时无需担心修改任何对象的状态,因为没有接收对象 - 它只是像自由函数一样被调用。

Perhaps it would help to have a simple code example.

class Foo {
public:
    static void static_function();
    void const_function() const;
};

// Use of static function:
Foo::static_function();

// Use of const function:
Foo f;
f.const_function();

The key difference between the two is that the const function is a member function -- that is, it is invoked on instances of the Foo class. That means you first need to instantiate an object of type Foo, and then that object acts as the receiver of the call to const_function. The const itself means that you won't modify the state of the object which is the receiver of that function call.

On the other hand, a static function is essentially a free function, where you can call it without a receiving object. Outside the scope of the class where it's defined, however, you'll need to qualify it using the class name: Foo::static_function.

This is why it doesn't make sense to have a function which is both static and const, as they're used in entirely different contexts. There's no need to worry about modifying the state of any object when invoking a static function because there is no receiving object -- it is simply invoked like a free function.

夏天碎花小短裙 2024-12-09 08:20:03

因为类的 static const 函数没有意义。 const 意味着事物(对象/变量)保持不变。静态意味着事物对象等在该上下文中保持不变。

Because a static const function of a class does not make sense. const means that a thing (object/variable) stays the same. Static means that a thing object etc stays the same in that context.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文