开发者

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:

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

it is giving me errors... modifiers not allowed o开发者_StackOverflow社区n 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.


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.


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.


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)


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)


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜