繁体   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