0

I need to write a few POD structs, but should I place them in a .h or in a .cpp file?

example

struct Vec2
{
    float x, y, z;
}

Should this be placed inside of Vec2.h or inside of Vec2.cpp?

  • 2
    Class definitions must go in a header if you intend to access them in another file, other than the corresponding cpp file. – Cory Kramer May 19 '16 at 18:17
  • 2
    It has nothing to do with what type of structure it is, it's about whether you want the same structure to be visible/usable from multiple .cpp files. If you're only going to use it in one .cpp, put it in that cpp, otherwise put it in a .h file. – kfsone May 19 '16 at 18:17
  • [Why have header files and .cpp files in C++?](https://stackoverflow.com/questions/333889/why-have-header-files-and-cpp-files-in-c) – Cory Kramer May 19 '16 at 18:17
  • Thanks for the information and the quick response! – Daniel Hop May 19 '16 at 18:20

1 Answers1

0

I need to write a few POD structs, but should I place them in a .h or in a .cpp file?

There's nothing in a real POD as you're showing it, that needs to be implemented explicitly in a separate translation unit (.cpp file).

Should this be placed inside of Vec2.h or inside of Vec2.cpp?

Just place

struct Vec2 {
    float x, y, z;
}; // <<<< note semicolon

in your header file, include it elsewhere and use it. There's no need for Vec2.cpp.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190