简体   繁体   中英

Client struct vs. implementation struct in .h file

Alright, this is basic c++ here. I've got a class that is a Linear Linked List. I want to declare two structs. One of them is for the client to use (ie. the info they are going to pass into the class) and the other struct is just for me, the implementer, to manage the linked list (this is the "node"). ie:

//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;
};

What I am confused about is exactly where to place these. I believe it is best to place the struct that the client is going to use above the class definition, but where is the best place to put the "node" struct. Should this go in private? Thanks guys!

The node structure could simply be placed into your .cpp file. If there's something in your header that refers to a node, eg "struct node *firstNode" then you'll have to forward declare it near the top of your header, just by saying "struct node;".

So, .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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM