簡體   English   中英

在C ++中將“ this”添加到類實例化的向量中

[英]Add “this” to a vector on class instantiation in C++

我來自Python背景,因此請原諒我。 盡管我將提供與我要尋找的Python等效的工具。

我正在創建一個網絡節點列表,因此我想創建一個類“ Node”,該類存儲它們的MAC,IP地址和主機名,以及一個將它們打印得很漂亮的函數。 以下是我的代碼:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Node {
    string MAC, IP, Hostname;
    public:
        void set_values(string M, string I, string H);
        string list() {return "MAC: "+MAC+"\nIP: "+IP+"\nHostname: "+Hostname+"\n";}
};

void Node::set_values(string M, string I, string H) {
    MAC = M;    
    IP = I;     
    Hostname = H;
}

int main(int argc, char* argv[])
{
    Node firstnode;
    firstnode.set_values("C0:FF:EE:C0:FF:EE","192.168.1.60","My-PC");
    cout<<firstnode.list();
}

當我運行它時可以打印出來:

MAC: C0:FF:EE:C0:FF:EE
IP: 192.168.1.60
Hostname: My-PC

我想要的是在創建時將這些對象自動添加到稱為NodeList的向量中。 例如,這是我在Python中執行的操作:

RecordersList=[]
class Recorder:
    def __init__(self, ARecorder, BRecorder, CRecorder):
        self.ARecorder = ARecorder
        self.BRecorder = BRecorder
        self.CRecorder = CRecorder
        RecordersList.append(self)

我嘗試了類似的操作,在其中放置了一行: vector<Node> NodeList; 在類聲明之前(和NodeList.push_back(this);作為Public函數),並在類聲明之后進行嘗試,但是無論哪種方式,編譯器在聲明矢量時都不知道Node類,反之亦然Node類不知道NodeList向量。

有沒有辦法做到這一點? 這將是自引用類,附加到類型屬於該類的現有向量上。

當然:在類中聲明並定義一個靜態成員,將this指針壓入該成員:

class Foo; // forward declaration to make vector happy

class Foo {
    private:
        static std::vector<Foo *> store;
    public:
        Foo() { store.push_back(this); }
};

std::vector<Foo *> Foo::store;

明確地做:

   std::map<std::string, Node> map;
   map[mac1] = Node(mac1,...);
   map[mac2] = Node(mac2,...);

以我的經驗,由於不得不手動管理C ++中的內存,這種設計通常無法很好地完成。 this是指向對象的原始指針,並且不受管理。

你可以這樣做:

class Node; // forward declaration
std::vector<Node*> NodeList;

class Node
{
public:

    Node()
    {
        NodeList.push_back(this); // pass a POINTER to this object
    }
};

int main(int argc, char* argv[])
{
    Node* node1 = new Node(); // allocated a Node
    Node* node2 = new Node(); // allocated a Node

    // ...

    // deallocate ALL nodes
    std::vector<Node*>::iterator it = NodeList.begin();
    while (it != NodeList.end())
    {
        delete *it;
        ++it;
    }

    NodeList.clear();
}

該解決方案的問題是,如果您有指向單個節點的指針。 您最終可能會懸空指針和內存損壞。

替代解決方案是:

class Node
{
public:

    Node();
};

std::vector<Node> NodeList;

Node::Node()
{
    NodeList.push_back(*this); // pass a REFERENCE to this object
}

int main(int argc, char* argv[])
{
    Node node1; // create a node
    Node node2; // create a node

    // ...
}

這種替代設計的問題在於,傳遞給NodeList每個節點都是該節點的新COPY。 因此,如果您這樣做:

int main(int argc, char* argv[])
{
    Node node1; // NodeList[0] is logically equal to node1

    node1.DoStuffThatModifiesTheContent();

    // At this point, node1 is no longer a logical equivalent of NodeList[0]
}

更好的設計包括創建某種NodeManager類,並通過該管理器創建和訪問節點,這將控制所有節點對象的生存期。

暫無
暫無

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

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