Programming style for declaring a variable method of a get / set method in C ++?

If you declare class getters / setters inside a .h file, then define them in .cpp or do both in a .h file. Which style do you prefer and why? I personally like the latter, in which they are all in .h and only the methods that are associated with it differently than the setters / getters in .cpp.

+6
c ++ coding-style
source share
8 answers

For me, it depends on who will use the .h file. If this is a file that is largely internal to the module, then I try to use tiny methods in the header. If this is a more external header file that contains a more fixed API, then I put everything in .cpp files. In this case, I often use PIMPL Idiom for a full compilation firewall.

The tradeoffs that I see putting them in the headers are as follows:

  • Smaller setting
  • A simple insert for the compiler (although compilers can sometimes do nesting between multiple translation units anyway.)
  • Additional dependencies for compilation
+12
source share

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

+8
source share

For me, it depends on what I do with the code. For the code that I want to keep and serve over time, I put everything in a .cc file for the following reasons:

  • An .h file can remain sparse as documentation for people who want to look for definitions of functions and methods.
  • My recommendations for group coding indicate that we put everything in a .cpp file and, like following it, even if the function definition takes only one line. This eliminates guessing games about where things really live, because you know which file you should study.
  • If you frequently recompile a large project, saving a function definition in a .cpp file saves you time compared to saving function definitions in header files. This was relevant recently for us, since we recently looked at the code and added many assert runtime statements to check the input for our classes, and this required a lot of modification for getters and seters. If these method declarations lived in .cpp files, this would turn into a clean recompilation for us, which could take about 30 minutes on my laptop.

This does not mean that sometimes I don’t play quickly and dirty with the rules and put things in .h files when implementing something very quickly, but for the code I am serious about the whole code (regardless of length) in the .cpp file. For large projects (some of them), the rules exist for some reason, and subsequent ones may be a virtue.

Speaking of this, I just thought about another Perl script, I can hack to find coding violations. Good to be popular. :)

+4
source share

I put all single liners in the header if they do not require too many extra headers (because they call methods of other classes). Also, I am not trying to put all the code on one line, so I can put most of the methods in the header :-)

But Josh mentioned a good reason to put them in .cpp anyway: if the header is for external use.

+2
source share

I prefer to keep the .h file as clean as possible. Therefore, I often use small functions that are as simple as get / set to place a single file as built-in functions, and then include this file (where I use the .inl extension) in the .h header file:

// foo.h class foo { public: int bar() const; private: int m_bar; }; #include "foo.inl" // foo.inl inline int foo::bar() const { return m_bar; } 

I think this gives the best of both worlds, while at the same time hiding most of the implementation from the header and still retains the advantage of creating simple code (as a rule, I keep it in no more than 3 statements).

+2
source share

I almost always follow the division of their declarations in the header and identifying them in the source. Every time I do not do this, I have to come back and do it later.

+1
source share

I prefer to put them in a .cpp file to speed up compilation / link time. Even tiny one-liners (empty virtual destructors!) Can explode your compilation times if they are created multiple times. In one project, I could reduce the compilation time by a few seconds by moving all the virtual destructors to .cpp files.

Since then I have been selling this, and I would only put them in the header again if the profiler tells me that I can benefit from inlining. The only drawback is that you need to type more, but if you create the .cpp file while recording the header, you can simply copy and paste the ads and fill them in the .cpp file, so it’s not so bad. Even worse, if you later find out that you want to move the material to a .cpp file.

A good side effect is that reading the material is easier when your title contains only documentation and declarations, especially if new developers join the project.

+1
source share

I use the following rule: header for declaration, code file for implementation. This becomes actual when your title is used outside the project - the lighter your title, the more convenient it is to use.

0
source share

All Articles