简体   繁体   English

访问抽象类派生类的抽象类的向量! 怎么样?

[英]Vector of an abstract class that access the derived classes of the abstract one ! How?

ACTUAL PROBLEM: have an Abstract Class operations that inherits from VAR Class , which then all the operations derived class(out,sleep,Add) inherit from the operations class. 实际问题:具有从VAR Class继承的Abstract Class操作,然后所有派生类的操作(out,sleep,Add)都从Operations类继承。 FSM Class inherits from Var also, so That I want one instance of VAR class inside my program. FSM类也从Var继承,因此我希望程序中有一个VAR类实例。

I am trying to make vector < pair< string, int>> var as a shared data between the FSM class and the Operations class and its deviates . 我试图使向量<pair <string,int >> var作为FSM类和Operations类之间的共享数据,并且它与devies偏离。 I initialized the var in the main through the FSM class . 我通过FSM类在主体中初始化了var。

Each time we call the exist function in VAR through Class operation , it returns it doesn't exits cause it is empty ! 每次我们通过Class操作在VAR中调用exist函数时,它返回的结果是不存在,因为它为空! How can I overcome this? 我该如何克服?

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class VAR
{
public:vector<pair<string, int>> var;
    VAR()
    {}
 ~VAR(){}
void createVar(string x,int y)
    {}
void setVarValue(string& x, int y)
    {}
int getVarValue(string x){}
 bool exits(string& name)
    {}
class operations : virtual public VAR
{
public:
    operations()
    {}
void virtual excute() = 0;    
};
class Out :public virtual operations
{
};
class Add :public  virtual operations
{
};

class FSM :public virtual VAR, public virtual transition
    {
       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
}

Not all shapes are usefully characterised by a length, so it doesn't make sense to want to get the length of all shapes. 并非所有形状都有用长度作为特征,因此想要获取所有形状的长度没有任何意义。 For example, a circle might have a radius or a diameter, but talking about the length of a circle would get blank looks from most people. 例如,一个圆可能有一个半径或直径,但是谈论一个圆的长度会使大多数人空白。

The usual point of a base class is that it provides functionality that are relevant to all shapes. 基类的通常要点是它提供与所有形状相关的功能。 So a member function like draw() may be relevant to all shapes (albeit, as a virtual function, each derived class can implement specifics of how it is drawn) but functions like setLength() and getLength() may not be. 因此,诸如draw()的成员函数可能与所有形状相关(尽管作为虚拟函数,每个派生类都可以实现其绘制方式的细节),但诸如setLength()getLength()类的函数却可能不相关。

Generally speaking, when starting from a pointer to base, if you need to convert it to a derived type, that is usually considered a sign of a broken design (and not just by C++ practitioners). 一般而言,当从指向基础的指针开始时,如果需要将其转换为派生类型,则通常将其视为破坏设计的标志(而不仅仅是C ++从业人员)。 What you really need to do is work out the set of capabilities the shape class really needs to provide, and make them general enough so it can handle the need for some specialised shapes to have a length, and a need for some other specialised shapes not to have a length. 您真正需要做的是找出shape类确实需要提供的功能集,并使它们足够通用,以便可以处理某些特殊形状具有一定长度的需求,而无需处理某些其他特殊形状有长度。

It's hard to give a more specific answer than that as your needs (and therefore the set of capabilities your shape class needs to provide) will be specific to your application. 很难给出比这更具体的答案,因为您的需求(以及您的shape类需要提供的功能集)将特定于您的应用程序。

Oh: main() returns int , not void in standard C++. 哦: main()返回int ,在标准C ++中不是void Your compiler may support void main() , but not all do, so it is usually considered a bad idea to use it. 您的编译器可能支持void main() ,但并非全部都支持,因此使用它通常被认为是一个坏主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM