静态成员函数

来源:互联网 发布:淘宝行业发展趋势 编辑:程序博客网 时间:2024/06/08 19:14
 

当遇到D:\360Downloads\MYPROJECT\Test\test.cpp(24) : error C2352: 'Point::output' : illegal call of non-static member function
        D:\360Downloads\MYPROJECT\Test\test.cpp(7) : see declaration of 'output'错误时

说明该成员(output)不是静态成员函数, 也就是(output)不是(static)修饰的成员函数。

小结: 静态成员函数和表态成员变量属于类本身, 在类加载的时候, 即为它们分配了空间,

所以可以通过类名::函数名或类名: 变量名来访问。而非静态成员函数和非静态成员属于对象

的方法和数据,也就是应该首先产生类的对象,然后通过类的对象去引用。


example:
#include <iostream>
using namespace std;

class Point
{
public:
  void output()
 {

 }
 static void init()
 {

 }
};

void main()
{
// Point pt;
// pt.init();
// pt.output();

 Point::init();
 Point::output();
}


还有注意 在静态成员函数中是不能调用非静态成员的, 包括非静态成员函数和非静态成员变量。

但是将非静态成员函数和非静态成员变量的赋值操作放到一个非静态成员函数中, 程序将会成功

生成执行文件。

原创粉丝点击