Jump to content
  • Advertisement
Sign in to follow this  
appel

Structs in C++

This topic is 4822 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey all, I have a silly question. Rarely have I used structs, but when I have used them I simply include them in the .cpp file. What is the right way to do this? Let's say I have a structure called Employee, do I create a file for it like; Employee.cpp or Employee.struct ? What is the right convention for structs? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
There is no other convention than he one you decide, but most of the time a struct which is reused over your project is declared in a header file (.h). If nothing else is related to this struct then it can be in an independant header file; otherwise (for example if the struct is used only when using a particular class) you can declare it in the class header.

Using other extensions that .h, .c, .cpp, .inl is not recommanded, though it is possible.

Regards,

Share this post


Link to post
Share on other sites
If the struct is only used in one file and it doesn't have any code of its own, then just declare it where it is used. Otherwise, the normal convention is to declare the struct in a "header" file (Employee.h), and to define its member functions in a "source" file (Employee.cpp).

This article is extremely helpful: Organizing Code Files in C/C++

Share this post


Link to post
Share on other sites
Well one way to do it would be say you could keep the declaration in a header file say struct.h , the implementation could be in the struct.cpp file , and the in the program which has the main would have to include struct.h , ie in a way , if the interface[Structure + function inside the structure] is constant , you can change the implementation of the structure without having to recompile the entire project .Well more of a modular approach i belive and it beneficial to large scale projects .

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!