不能使用“静态” c++ 中静态方法的关键字类实现文件(.cpp)
考虑:
// In Vector2.h
class Vector2
{
public:
// returns the degrees in radians
static double calcDir(double x, double y);
}
// In Vector2.cpp
double Vector2::calcDir(double x, double y)
{
double rad = ...;
return rad;
}
为什么 Vector2.cpp 的签名中不需要关键字 static ?当我尝试这样做时,它会产生一个错误:
static double Vector2::calcDir(double x, double y)
它对我来说似乎不一致。方法签名的所有其他部分都需要在 .cpp 文件中重复(返回类型、方法名称(废话)、参数的名称和类型、const 性)。我不喜欢一眼就知道方法是否是静态的(在查看实现时)。
有什么理由不仅不需要而且禁止这样做吗?
Consider:
// In Vector2.h
class Vector2
{
public:
// returns the degrees in radians
static double calcDir(double x, double y);
}
// In Vector2.cpp
double Vector2::calcDir(double x, double y)
{
double rad = ...;
return rad;
}
Why isn't the keyword static required in the signature in Vector2.cpp? When I try this, it produces an error:
static double Vector2::calcDir(double x, double y)
It seems inconsistent to me. All other parts of the method signature are required to be repeated in the .cpp file (return type, method name (duh), names and types of args, const-ness). I don't like not knowing at a glance whether a method is static or not (when looking at the implementation).
Is there a reason this is not only not required, but forbidden?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为
static
在类定义中使用时具有特殊含义。在类定义中,它将函数标识为静态成员函数,这意味着它不对类实例进行操作,但可以独立调用。在类外部
static
提供函数内部链接,但这对于(即使是静态)成员函数来说是非法的,因为类成员必须与它们所属的类具有相同的链接(几乎总是外部的,当然,如果您可以在类定义之外定义成员函数,则当然是外部的)。从语言的角度来看,类定义中的成员声明遵循一组语言规则,其中
static
具有其特殊的类含义。在类定义之外,所有函数定义(成员和非成员)都遵循同一组规则,其中static
具有其他含义,这对于具有外部链接的类的成员无效。It's because
static
has a special meaning when used in a class definition. In a class definition it identifies the function as a static member function which means it doesn't operate on a class instance but can be called independently.Outside of a class
static
gives a function internal linkage but this is illegal on (even static) member functions because class members must have the same linkage as the class of which they are a member (almost always external, and certainly external in cases where you can defined member functions outside of the class definition).From a language point of view, the declaration of members inside a class definition follows one set of language rules where
static
has its special class meaning. Outside of a class definition, all function definitions - members and non-members - follow the same set of rules wherestatic
has it's other meaning which is not valid for members of classes with external linkage.这可能是为了减少混乱。在文件范围内,关键字static(令人困惑地)用于表示“使用内部链接”(TC++PL p.200)。这可能意味着该函数虽然是类的成员,但仅在当前翻译单元内可见。允许 static 说明符会更加混乱。
请注意,不再建议使用静态来表示内部链接,并且应首选匿名命名空间来实现这一点。
This is probably to reduce confusion. At file scope, 'the keyword static is (confusingly) used to mean "use internal linkage" (TC++PL p.200). It could imply that the function, though a member of a class, was only visible inside the current translation unit. Allowing the static specifier there would be more confusing.
Note that using static to denote internal linkage is no longer recommended, and that anonymous namespaces should be preferred to achieve that.
令人烦恼的是,关键字最终调用了两个截然不同的功能(全局到所有类实例与本地到文件),但以下小标头可能有用。该宏只是扩展为一个空字符串,这意味着您可以将其添加到实现文件中方法声明的前面,并在一年后返回您的代码,您将避免通过 2000 行“boost-esque”标头倾倒找出为什么编译器不允许你使用它。
staticmethod.h:
在您的 .cpp 文件中,您可以使用:
It is annoying that keyword wound up invoking two very different functionalities (global-to-all-class-instances vs. local-to-file) but the following small header may be of use. The macro just expands to an empty string meaning you can tack it in front of the declarations of methods in the implementation file and coming back to your code a year later you'll avoid pouring through a 2000-line "boost-esque" header to find out why the compiler won't let you use this.
staticmethod.h:
In your .cpp files you could then use: