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

标签 c++ class struct

好的,这是基本的 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/

相关文章:

c++ - 对`cv::VideoCapture::VideoCapture(int) 的 undefined reference

android - JNI : map jobject to native c++ object

.net - 在 matlab 中使用 .net 自定义类

C 指针、结构和 fwrite

c++ - std::vector size()-1 是否总是给出最后一个元素的索引?

c++ - 为特定的读写操作重载下标运算符

c++ - 友元函数和命名空间。无法访问类中的私有(private)成员

c++ - 在函数中访问类的数组

c++ - 检查结构数组是否为空

从嵌套函数调用结构指针