简体   繁体   English

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

[英]Declaring C++ member function as static const yields errors

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: Test.cpp包含fun()的实现:

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

it is giving me errors... modifiers not allowed on static member functions 它给了我错误... 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. 我想知道为什么我无法创建静态和const的函数。

void fun() const;

means that fun can be applied to const objects (as well as non const). 意味着fun可以应用于const对象(以及非const)。 Without the const modifier, it can only be applied on non const object. 没有const修饰符,它只能应用于非const对象。

Static functions by definition need no object. 根据定义,静态函数不需要对象。

A member function being const means that the other non-const members of the class instance can't be called . const成员函数意味着无法调用类实例的其他非const成员

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 . 自由函数不是成员函数,因此它与类或类实例无关,因此它不能是const,因为没有成员

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. 在最后两种情况下,没有使用const访问的意义,因为没有成员可以访问。

Static functions work without an instance, whereas const guarantees that the function will not change the instance (even though it requires an instance). 静态函数在没有实例的情况下工作,而const保证函数不会更改实例(即使它需要实例)。

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. 因此, static void fun() const有两个相互矛盾的含义。

BTW: This translation occurs for all member functions (const or not) 顺便说一句:这个转换发生在所有成员函数中(const或不是)

i answered this a few hours ago here: Why we need to put const at end of function header but static at first? 几个小时前我在这里回答了这个问题: 为什么我们需要将const放在函数头的末尾但是首先是静态的?

(SO system is not happy with my response. automatically converted to comment) (SO系统对我的回复不满意。自动转换为评论)

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. 两者之间的关键区别在于const函数是一个成员函数 - 也就是说,它是在Foo类的实例上调用的。 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 . 这意味着您首先需要实例化Foo类型的对象,然后该对象充当对const_function的调用的const_function The const itself means that you won't modify the state of the object which is the receiver of that function call. const本身意味着您不会修改作为该函数调用的接收者的对象的状态。

On the other hand, a static function is essentially a free function, where you can call it without a receiving object. 另一方面, static函数本质上是一个自由函数,您可以在没有接收对象的情况下调用它。 Outside the scope of the class where it's defined, however, you'll need to qualify it using the class name: Foo::static_function . 但是,在定义它的类的范围之外,您需要使用类名限定它: 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. 这就是为什么有一个staticconst的函数是没有意义的,因为它们在完全不同的上下文中使用。 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. 有没有必要担心调用时修改任何对象的状态static函数,因为没有接收对象-这只不过是调用像一个免费的功能。

Because a static const function of a class does not make sense. 因为类的static const函数没有意义。 const means that a thing (object/variable) stays the same. const表示事物(对象/变量)保持不变。 Static means that a thing object etc stays the same in that context. 静态意味着事物object等在该上下文中保持不变。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM