为什么静态成员函数只能在类定义中声明为静态,而不能在其自己的定义中声明为静态? [英] Why can a static member function only be declared static inside the class definition and not also in its own definition?

查看:99
本文介绍了为什么静态成员函数只能在类定义中声明为静态,而不能在其自己的定义中声明为静态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实现用于在屏幕上创建/更新框的类时,我想添加一个静态成员函数,以确保没有当前可见的框重叠(将其信息从静态指针数组获取到所有当前可见的框)

While implementing a class for creating/updating boxes on the screen, I wanted to add a static member function that makes sure no currently visible boxes overlap (taking its information from a static pointer array to all currently visible boxes)

我的初始代码具有以下结构:

My initial code had the following structure:

class Box
{
public:
    // ...
    static void arrangeOverlappingBoxes();
};

static void Box::arrangeOverlappingBoxes()
{
    // ...
}

我很惊讶这会产生错误C2724:不应在文件范围内定义的成员函数上使用静态".

I was quite surprised that this generated an error C2724: 'static' should not be used on member functions defined at file scope.

通过一些试验,谷歌和错误,我发现我的函数定义应该丢失关键字 static ,即应该是

With some trial, google and error, I figured out that my function definition should lose the keyword static, i.e. it should be

void Box::arrangeOverlappingBoxes()
{
    // ...
}

但是我不知道背后的原理是什么.在类定义和它自己的定义中,它的声明似乎具有不同的函数标头,看起来似乎是不对称的,令人困惑.有什么理由吗?

Yet I have no clue what the rationale behind this could be. It appears to be so asymetric and confusing to have a different function header for its declaration in the class definition and its own definition. Is there any reason for this?

推荐答案

您的类定义(在头文件中)将为函数提供所需的属性:

Your class definition (in the header file) will provide the function with whatever propreties are necessary :

  • 静态
  • 内联
  • 虚拟

考虑到每个其他对象都将使用.h查看您的类定义,因此有必要在此处定义这些属性.

Considering that every further object will look at your class definition using the .h then it makes sense that these properties to be defined there.

此外,该类中的每个函数都将保留其在派生类中的属性(例如,您只需要在基类中将析构函数声明为virtual,随后的每个继承都将析构函数视为virtual.)

Furthermore, each function from the class will mentain it's property in the derived classes (for example you need to declare the destructor virtual only in your base class, every subsequent inheritance will take the destructor as virtual).

在实现主体中重新声明这些属性是没有意义的.

It makes no sense to redeclare these properties in your implementation body .

必须在.h和.cpp文件中声明函数专有性实际上会导致问题分配.想象一下这种情况:您在.h文件中将函数声明为虚函数,而在.cpp文件中将函数声明为静态.编译器将如何实现该功能?是虚拟的还是静态的?(或更可能是编译错误,但是编译器错误只会促使您在.cpp文件中匹配标头中的声明.不能根据静态"或虚拟"重载函数.)

Having to declare function proprieties in both .h and .cpp files would actually lead to allot of problems. Imagine this scenario : you declare a function as virtual in a .h file, and as static in the .cpp file. What will the compiler make that function ? virtual or static ? (or more likely a compile error , but the compiler error will just urge you to match in your .cpp file the declaration in the header. You cannot overload a function according to "static" or "virtual").

这篇关于为什么静态成员函数只能在类定义中声明为静态,而不能在其自己的定义中声明为静态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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