gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:02:21 25 4
gpt4 key购买 nike

好的,这是基本的 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 文件中。如果您的 header 中有指向节点的内容,例如“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
}

关于c++ - .h 文件中的客户端结构与实现结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10216130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com