Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programming style of method declaration of get/set method variables in C++?

Should you declare the getters/setters of the class inside the .h file and then define them in .cpp Or do both in .h file. Which style do you prefer and why? I personally like the latter wherein all of them are in .h and only methods which have logic associated with it other than setters/getters in .cpp.

like image 325
kal Avatar asked Jan 24 '09 18:01

kal


3 Answers

For me it depends on who's going to be using the .h file. If it's a file largely internal to a module, then I tend to put the tiny methods in the header. If it's a more external header file that presents a more fixed API, then I'll put everything in the .cpp files. In this case, I'll often use the PIMPL Idiom for a full compilation firewall.

The trade-offs I see with putting them in the headers are:

  • Less typing
  • Easier inlining for the compiler (although compilers can sometimes do inlining between multiple translation units now anyway.)
  • More compilation dependencies
like image 120
Eclipse Avatar answered Oct 11 '22 13:10

Eclipse


I would say that header files should be about interface, not implementation. I'd put them in the .cpp.

like image 22
duffymo Avatar answered Oct 11 '22 12:10

duffymo


For me this depends on what I'm doing with the code. For code that I want maintained and to last over time, I put everything in the .cc file for the following reasons:

  • The .h file can remain sparse as documentation for people who want to look for function and method definitions.
  • My group's coding guidelines state that we put everything in the .cpp file and like to follow those, even if the function definition only takes one line. This eliminates guessing games about where things actually live, because you know which file you should examine.
  • If you're doing frequent recompiles of a big project, keeping the function definition in the .cpp file saves you some time compared to keeping function definitions in header files. This was relevant very recently for us, as we recently went through the code and added a lot of runtime assert statements to validate input data for our classes, and that required a lot of modification to getters and setters. If these method declarations had lived in .cpp files, this would have turned into a clean recompile for us, which can take ~30min on my laptop.

That's not to say that I don't play fast-and-dirty with the rules occasionally and put things in .h files when implementing something really fast, but for code I'm serious about I all code (regardless of length) in the .cpp file. For big projects (some of) the rules are there for a reason, and following them can be a virtue.

Speaking of which, I just thought of yet another Perl script I can hack together to find violations of the coding guidelines. It's good to be popular. :)

like image 27
James Thompson Avatar answered Oct 11 '22 14:10

James Thompson