继承,我怎样才能使两个类共享相同的基类内容?

时间:2015-04-06 13:16:36

标签: c++ class inheritance

我有一个继承自VAR类的抽象类操作,然后所有操作派生类(out,sleep,Add)都从操作类继承。 FSM类也继承自Var,所以我希望在我的程序中有一个VAR类实例。

我正在尝试制作矢量<对< string,int>> var作为FSM类与Operations类及其偏差之间的共享数据。我通过FSM类初始化了main中的var。

每次我们通过Class操作调用VAR中的exists函数时,它返回它不会退出因为它是空的!我怎么能克服这个?

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class VAR
{
public:
    vector<pair<string, int>> var;
    VAR()
    {
        cout << "created VAR" << endl;
    }

    ~VAR(){ cout << "Destrioed  VAR" << endl; }
    void createVar(string x,int y)
    {
        pair<string, int>t;
        t.first = x;
        t.second = y;
        var.push_back(t);
    }
    int getVarValue(string x)

    {
        for (int i = 0; i<var.size(); i++)
        {
            if (var[i].first == x)
            {
                return var[i].second;
            }
        }
    }
    void setVarValue(string& x, int y)
    {
        for (int i = 0; i<var.size(); i++)
        {
            if (var[i].first == x)
            {
                var[i].second = y;
                i = var.size();
            }
        }
    }

    bool exits(string& name)
    {
        for (int i = 0; i<var.size(); i++)
        {
            if (var[i].first == name)
                return true;
        }
        return false;

    }
};
class operations : virtual public VAR
{
public:
    operations()
    {
        cout << "operations created" << endl;
    }
    ~operations()
    {
        cout << "operations   Destroied" << endl;
    }
    void virtual excute() = 0;

};

class Out :public virtual operations
{
public:
    string s;
    Out(string xx = "") :s(xx)
    {
        cout << "Out created" << endl;
    }
    ~Out()
    {
        cout << "Out   Destroied" << endl;
    }
    void virtual excute()
    {
        cout << "out Class" << endl;

        if (exits(s))
        cout<<"it never reach here, WHY !"<<endl;
    }
};
class Add :public  virtual operations
{
public:
    string s;
    Add(string ss = "") :s(ss)
    {
        cout << "ADD created" << endl;
    }
    ~Add()
    {
        cout << "Add   Destroied" << endl;
    }
    void virtual excute()
    {
        string ex1 = s.substr(s.find('=') + 1, s.find('+')), ex2 = s.substr(s.find('+') + 1);
        if (exits(ex1))
            cout<<"it never reach here, WHY !"<<endl;
        else
            result = atoi(ex1.c_str());

        if (exits(ex2))
            cout<<"it never reach here, WHY !"<<endl;

    }
};
class state
{
public:
    vector<operations*> instructionList;
    string name;
    void exec_all()
    {
        for (int x = 0; x < instructionList.size(); x++)
            instructionList[x]->excute();
    }
};

class transition
{
public:
    vector < pair<state, vector<pair<state, int>>>> trans;
    static int currentState;
};
class FSM :public virtual VAR, public virtual transition
{
public:
    FSM()
    {
        cout << "FSM" << endl;
    }
    void intialize()
    {
        createVar("X", 1);
        createVar("Y", 5);
    }
};
void main()
{
    FSM x;
    pair<state, vector<pair<state, int>>> p1;
    pair<state, int>p2;
    x.intialize();

    p2.first.name = "b";
    p2.second = 3;
    p1.first.name = "a";

    p1.second.push_back(p2);
    x.trans.push_back(p1);

    x.trans[0].first.instructionList.push_back(new Add("X=X+Y"));
    x.trans[0].first.instructionList.push_back(new Out("X"));
    x.trans[0].first.exec_all();//wrong output cause exist() returns false

}

1 个答案:

答案 0 :(得分:0)

最小的完整示例如下所示:

#include <iostream>
using namespace std;

class VAR
{
public:
  int var;

  virtual ~VAR()
  {}

  void setVar(int n)
  {var=n;}
};

class Out :public VAR
{};

class FSM :public VAR
{};

int main()
{
  FSM x;
  x.setVar(5);

  Out OP;

  if (x.var==OP.var)
    cout<<"it reaches here now" << endl;
  else
    cout << "it fails" << endl;

  return(0);
}

解决问题的一种方法是:

class VAR
{
public:
  static int var;
  int var;

  virtual ~VAR()
  {}

  void setVar(int n)
  {var=n;}
};

int VAR::var=0;