您的位置:首页 > 其它

static类型的父类成员变量,与所有资料共享该变量

2015-03-16 16:37 176 查看
#include <iostream>

#include "string.h"

using namespace std;

class Base

{

public:

Base(){s=5;};

/*static*/ int s ;//当用静态成员变量时,下面的继承的子类都共享真个变量

};

//int Base::s=5;

class Derived1 : public Base

{

};

class Derived2 : public Base

{

};

void main()

{

Base b;

Derived1 d1;

Derived2 d2;

cout<<b.s<<endl;

d1.s=7;

b.s=9;

cout<<b.s<<endl;

cout<<d1.s<<endl;

cout<<d2.s<<endl;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐