C++實現全局可讀,特定類可寫

關於實現以下功能的代碼。
1.變量i可以通過Atest:getint()得到。
2.變量i只允許繼承了他的B修改。
3.維持B員工能不變。

如果哪位大神有更好的方法請告訴我 謝謝。

// test.cpp : 定義控制檯應用程序的入口點。
//

#include "stdafx.h"
#include "iostream"
#include "memory"

using namespace std;

class Atest
{
public:
    static int getint(){return i;}
protected:
    void setint(int value)
    {
        i = value;
    }
    private: 
     static int i ;

};


class Ctest
{
public:
    int ztest()
    {
        cout << "asd" << endl;
        return 0;
    };

};


class Btest :public Atest,public Ctest
{
public:
    Btest(){};
    ~Btest(){};

    void test()
    {

        cout << getint() << endl;
        setint(5);
        cout << getint() << endl;
        //cout << Atest::i << endl;
    }

    void ssss()
    {
        setint(3);
    }
};


int Atest::i ;

int _tmain(int argc, _TCHAR* argv[])
{



    Btest * B = new Btest();
    B->ssss();
    B->test();
    B->ztest();
    cout << Atest::getint() << endl;
    while (1)
    {

    }
    return 0;
}

發佈了50 篇原創文章 · 獲贊 38 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章