2015-04-06 61 views
0

我有一個從VAR類繼承的抽象類操作,然後所有操作的派生類(out,sleep,Add)從操作類繼承。 FSM類也從Var繼承,所以我希望我的程序中有一個VAR類的實例。繼承,我如何讓兩個類共享相同的基類內容?

我試圖使矢量<對< string,int >> var作爲FSM類和Operations類及其偏差之間的共享數據。我通過FSM類初始化了main中的var。

每次我們通過Class操作調用VAR函數中的存在函數時,都會返回它不存在導致它爲空!我該如何克服這一點?

#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 

} 
+3

TL; DR!請創建一個[最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)以顯示您想要的內容。 – 2015-04-06 13:19:18

+0

「Wait」沒有像其他類一樣繼承嗎? – Jarod42 2015-04-06 13:21:54

+0

導致等待不需要變量,即使我們使其從VAR繼承,也可以。 – 2015-04-06 13:33:47

回答

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; 
+0

爲什麼要這樣做:static int var – 2015-04-06 21:50:32

+0

它工作正常後,我宣佈載體爲靜態。非常感謝@Beta – 2015-04-07 08:36:40