当前位置: 代码迷 >> 综合 >> 类的静态函数 静态成员(类的静态成员 属于 类)
  详细解决方案

类的静态函数 静态成员(类的静态成员 属于 类)

热度:22   发布时间:2023-10-14 03:22:19.0
#include <iostream>
using namespace std;
class school
{public:
    school(string t,string l)
    {tower=t;lake=l;}
    static string &getLib()//私有东西返回
    {return lib;}
    void dis()
    {cout<<"ta:"<<tower<<"hu:"<<lake<<"  tushuguan:"<<lib<<endl;}
private:
    string tower;
    string lake;
    static string lib;
};
string school::lib="";
int main()
{
  
    school   pk("boyata    ","weMeiHu ");
    school   bs("dianshiTa ","daminghu");
    school  xdf("leifengta ","xihu    ");
     //==============
 
 
//        pk.lib  +="aaa ";//我感觉不属于 类对象
//          pk.dis();
//         bs.lib  +="bbb ";
//          bs.dis();
//         xdf.lib +="ccc ";
//          xdf.dis();
     //==============
      school::getLib()+="aaa ";//类的静态成员属于类
        pk.dis();
      school::getLib()+="bbb ";
        bs.dis();
      school::getLib()+="ccc ";
        xdf.dis();
    cout << "Hello World!" << endl;
    return 0;
}

  相关解决方案