.h 文件中的客户端结构与实现结构

Client struct vs. implementation struct in .h file

本文关键字:结构 实现 客户端 文件      更新时间:2023-10-16

好吧,这是基本的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;
};

我感到困惑的是将这些放置在哪里。 我相信最好将客户端将使用的结构放在类定义上方,但是放置"节点"结构的最佳位置在哪里。 这应该私下进行吗? 谢谢大家!

节点

结构可以简单地放入您的.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
}