为什么静态成员函数定义不能使用关键字“静态”? [英] Why can static member function definitions not have the keyword 'static'?

查看:160
本文介绍了为什么静态成员函数定义不能使用关键字“静态”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照C ++中 static关键字上的链接:

As per this link on the 'static' keyword in C++ :


static关键字仅用于在类定义中声明静态
成员,而不用于静态成员
的定义

为什么在成员函数定义中禁止使用static关键字?我确实知道,在定义时将函数重新声明为静态是多余的。但是使用它在函数定义的编译期间应该是无害的,因为它不会导致任何歧义。那么,为什么编译器禁止这样做呢?

Why is the static keyword prohibited on member function definitions? I do understand that re-declaring a function as 'static' at its definition is redundant. But using it should be harmless during compilation of the function definition as it does not result in any kind of ambiguity. So why do compilers prohibit it?

推荐答案

有歧义。

请考虑以下问题:

namespace foo {
    static void bar();
}

static void foo::bar() {

}

$ b b b

foo :: bar 必须进行定义,并使用相同的链接说明符。

foo::bar is required to be defined with the same linkage specifier.

对于成员函数,静态不是链接说明符。如果允许的话, foo :: bar 定义的正确性将是非常上下文,具体取决于 foo 是。实际上,不允许 static 减轻了编译器的负担。

For member functions, however, static is not a linkage specifier. If it was allowed, the correctness of the definition of foo::bar will be very very context dependent on what foo is. Disallowing static in fact eases the burden on the compiler.

将其扩展到一般成员,而不是仅仅成员函数是一个一致性问题。

Extending it to members in general, as opposed to just member functions, is a matter of consistency.

这篇关于为什么静态成员函数定义不能使用关键字“静态”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆