C++类使用static小例子(新手学习C++)
//为什么类的成员中不能包括动态分配的数据,若包含静态数据怎么使用?
#include <iostream>
using namespace std;
class point
{
private:
static int xres;
int yres;
public:
point(int = 0,int = 0);
void set_point(int, int) ;
void get_point();
};
point::point(int x, int y)
{
// xres = x;
yres = y;
}
void point:: set_point(int x, int y)
{
// xres = x;
yres = y;
}
void point::get_point()
{
cout << "x = " << xres << endl;
cout << "y = " << yres << endl;
}
int point:: xres = 11; //此处楼主已验证,只有静态的public型变量才可以如此赋值
int main()
{
point p(10, 10);
point p1(30, 20);
cout << &p << endl;
cout << &p1 << endl;
p.get_point();
p1.get_point();
cout << point::xres;
return 0;
}