簡體   English   中英

Distinc派生類C ++

[英]Distinc derived class C++

我有一個基類和兩個派生類。 現在,我想創建一個基類的向量。 當我添加新元素時,它將檢查新元素的哪個類。 但是在C ++中,沒有像instanceOf這樣的函數,因此我必須添加變量type來進行檢查。 這是我的代碼。

class Element {
public:
    int type;
    int getIndex() const {
        return index;
    }

    void setIndex(int index) {
        this->index = index;
    }

    const std::string& getMsg() const {
        return msg;
    }

    void setMsg(const std::string& msg) {
        this->msg = msg;
    }

    std::string toString() {
        std::stringstream sbuf;
        sbuf << "index: " << index << "\n";
        sbuf << "Message: " << msg << "\n";
        return sbuf.str();
    }

private:
    int index;

    std::string msg;

};

兩個派生類:

class This: public Element {
public:
    This():Element(1){}
    ~This();

    const std::vector<std::string>& getArguments() const {
        return arguments;
    }

    void setArguments(const std::vector<std::string>& arguments) {
        this->arguments = arguments;
    }

    void add(std::string value) {
        arguments.push_back(value);
    }

private:
    std::vector<std::string> arguments;


};

class That: public Element {
public:
    That():Element(2){}
    ~That();

    const std::string& getThatVal() const {
        return thatVal;
    }

    void setThatVal(const std::string& thatVal) {
        this->thatVal = thatVal;
    }

private:
    std::string thatVal;


};

在另一個類中,我想創建Element數組。

class Visitor {
private:
    int numOfThis = 0;
    int numOfThat = 0;
    std::vector<Element> vecEle;

public:
    void add(Element ele) {
        vecEle.push_back(ele);
        if (ele.type == 1) {
            numOfThis++;
        } else if (ele.type == 2) {
            numOfThat++;
        }
    }

    int getNumOfThat() const {
        return numOfThat;
    }

    int getNumOfThis() const {
        return numOfThis;
    }
};

我的問題是如何更好地處理這個問題? 有這種情況的設計模式嗎? 謝謝

您可以將指針而不是對象存儲在向量中,如下所示:

std::vector<Element *> vecEle;

然后,您可以使用dynamic_cast來確定哪個派生類是指針的類型。

// dynamic_cast will try to convert pointer of base class to derived class, 
// and return NULL on fail
Element *pElem= vecEle[0]; // you can traverse your vector if needed
if (This *pThis = dynamic_cast<This *>(pElem)) {
    // object is This type
} else if (That *pThat = dynamic_cast<That *>(pElem)) {
    // object is That type
}

類元素應該是一個包含純虛擬函數的基類,即所謂的VisitElement( Visitor* visitor)

Visitor類與您在代碼中描述的類無關,它實際上是作為稱為Visitor的設計模式實現的。 這意味着它包含重載函數

void Visit(This* thisElem) {thisElem->VisitElement(this)}

void Visit(That* thatElem){thatElem->VisitElement(this)}

此Visitor類還將包含int counterThisint counterThat 在此類中,VisitElement的實現非常簡單

VisitElement( Visitor* visitor){ visitor->counterThis++;}

在課堂上VisitElemnt的實現很簡單

VisitElement (Visitor* visitor){ visitor->counterThat++;}

最后,為了真正計數,您需要遍歷基類Element指針的向量,並visitor->Visit(elemFromBector)向量調用中包含的每個指針visitor->Visit(elemFromBector) 循環完成后,您可以查詢訪問者對象以獲取所需的兩個int。 請記住,此解決方案涉及多態性和訪問者設計模式以及您似乎在原始代碼中缺少的某些封裝原理。

不要使用dynamic_cast,它是無效的,並且不應在任何生產代碼中使用。 如果您認為需要dynamic_cast,請再三考慮,因為您的代碼有問題。 最好的建議是有關多態性封裝訪問者設計模式的知識 ,您應該能夠很容易地理解我的邏輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM