簡體   English   中英

.h文件中的客戶端結構與實現結構

[英]Client struct vs. implementation struct in .h file

好吧,這是基本的c ++。 我有一個類是線性鏈接列表。 我想聲明兩個結構。 其中一個是客戶端使用(即他們要傳遞給類的信息),另一個結構只是我(實施者)管理鏈表(這是“節點”)。 即:

//this is the one for client use
struct form {
    char *name;
    int age;
    char *address;
    //etc.
};

struct node {
    char *name; //same as in above but I am sorting the LL by this so I need it out
    form *client_form;
    node *next;
};

我感到困惑的是它們究竟放在哪里。 我認為最好將客戶端將要使用的結構放在類定義之上,但是放置“node”結構的最佳位置在哪里。 這應該私有化嗎? 多謝你們!

節點結構可以簡單地放入.cpp文件中。 如果你的標題中有一些東西引用了一個節點,例如“struct node * firstNode”那么你只需要通過說“struct node;”就可以將它聲明轉發到標題的頂部附近。

那么,.h:

struct node;
struct form {
   // form fields
};

class MyStuff {
   private:
      struct node *firstNode;
   // more Stuff
};

的.cpp:

struct node {
   // node fields
};

MyStuff::MyStuff(struct form const& details) {
    // code
}

暫無
暫無

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

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