Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #11904

Re: .h and .cpp

From Rui Maciel <rui.maciel@gmail.com>
Newsgroups comp.lang.c++
Subject Re: .h and .cpp
Date 2011-11-28 20:07 +0000
Organization Aioe.org NNTP Server
Message-ID <jb0pmf$fn$1@speranza.aioe.org> (permalink)
References <d30834a8-3bf4-49a4-b1eb-b66aef965475@i8g2000vbh.googlegroups.com>

Show all headers | View raw


Christopher wrote:

> If you were on a team of developers that had little C++ experience,
> how would you go about explaining the reasoning as to why the
> following rules should be followed? (Or why they shouldn't)
> 
> 1) Class defintions go in their own .h (or .hpp for templates) file
> named after the class
> 1a) Every class should have its own .h (or .hpp for templates) file
> 1b) A .h file should never contain class implementation (except for
> inline functions or methods)
> 
> 2) Class implementations go in their own .cpp file (except for
> templates)
> 2a) A .cpp should never contain a class definition
> 
> 3) One should not define multiple classes in a single .h file
> 3a) Nested class defintions should be avoided without good reason
> (what's good reason?)
> 
> 6) Variables should never be defined in a .h file
> 7) Globals should not be defined in a .h file
> 8) Every .cpp file should compile into a .obj file without error or
> warning on its own.
>      i.e In Visual Studio, you should be able to right click a .cpp in
> the source tree and choose compile, without error or warning in the
> output.
> ----
> 
> Sadly, I find myself having to justify these things. (of course
> someone might point out I am wrong or exceptions to the above), but
> these are things that I've always had drilled into my head throughout
> the years and have rarely come acorss other developers that didn't do
> the same. I am finding it frustrating to work on a project where 30
> classes defintions reside in the .h same .h file, a single .cpp file,
> or worse a precompiled header. There is discussion, but I have to
> explain reasoning.

Separating declarations from implementations and mandating that each class 
should be defined and implemented in separate source files are a couple of 
practices which help track what changes were made to each individual 
component and who made those changes.  With this, auditing the code becomes 
a bit simpler.

This practice also reduces the amount of potential merging conflicts any 
commit might induce.


Rui Maciel

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

.h and .cpp Christopher <cpisz@austin.rr.com> - 2011-11-28 09:20 -0800
  Re: .h and .cpp Leigh Johnston <leigh@i42.co.uk> - 2011-11-28 17:30 +0000
    Re: .h and .cpp Christopher <cpisz@austin.rr.com> - 2011-11-28 10:39 -0800
      Re: .h and .cpp Tobias Müller <troplin@bluewin.ch> - 2011-11-28 19:09 +0000
  Re: .h and .cpp ralph <nt_consulting64@yahoo.net> - 2011-11-28 12:37 -0600
    Re: .h and .cpp Christopher <cpisz@austin.rr.com> - 2011-11-28 10:48 -0800
      Re: .h and .cpp ralph <nt_consulting64@yahoo.net> - 2011-11-28 14:24 -0600
      Re: .h and .cpp yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2011-11-30 11:24 +0000
        Re: .h and .cpp Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-01 21:42 +0000
          Re: .h and .cpp Rui Maciel <rui.maciel@gmail.com> - 2011-12-02 02:03 +0000
            Re: .h and .cpp Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-12-02 20:59 +0000
  Re: .h and .cpp "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-28 19:45 +0100
    Re: .h and .cpp Leigh Johnston <leigh@i42.co.uk> - 2011-11-28 19:03 +0000
      Re: .h and .cpp "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-28 20:36 +0100
    Re: .h and .cpp Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-11-28 22:52 +0000
      Re: .h and .cpp yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2011-11-30 11:39 +0000
        Re: .h and .cpp Juha Nieminen <nospam@thanks.invalid> - 2011-11-30 12:50 +0000
          Re: .h and .cpp yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2011-11-30 13:57 +0000
            Re: .h and .cpp Juha Nieminen <nospam@thanks.invalid> - 2011-11-30 15:06 +0000
          Re: .h and .cpp Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-11-30 16:21 +0000
        Re: .h and .cpp Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-11-30 16:12 +0000
  Re: .h and .cpp Goran <goran.pusic@gmail.com> - 2011-11-28 10:58 -0800
    Re: .h and .cpp Christopher <cpisz@austin.rr.com> - 2011-11-28 13:30 -0800
      Re: .h and .cpp Tobias Müller <troplin@bluewin.ch> - 2011-11-29 06:44 +0000
      Re: .h and .cpp Goran <goran.pusic@gmail.com> - 2011-11-29 01:17 -0800
      Re: .h and .cpp Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-30 00:53 -0800
    Re: .h and .cpp "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2011-11-29 10:57 +0100
    Re: .h and .cpp yatremblay@bel1lin202.(none) (Yannick Tremblay) - 2011-11-30 12:05 +0000
  Re: .h and .cpp Rui Maciel <rui.maciel@gmail.com> - 2011-11-28 20:07 +0000
  Re: .h and .cpp Geoff <geoff@invalid.invalid> - 2011-11-28 12:17 -0800
    Re: .h and .cpp Ian Collins <ian-news@hotmail.com> - 2011-11-29 11:41 +1300
      Re: .h and .cpp gwowen <gwowen@gmail.com> - 2011-11-29 07:37 -0800
    Re: .h and .cpp Goran <goran.pusic@gmail.com> - 2011-11-29 00:08 -0800
    Re: .h and .cpp Daniel <danielaparker@gmail.com> - 2011-11-29 08:31 -0800
      Re: .h and .cpp ralph <nt_consulting64@yahoo.net> - 2011-11-29 14:49 -0600
        Re: .h and .cpp Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-11-30 00:21 +0000
  Re: .h and .cpp Andy Champ <no.way@nospam.invalid> - 2011-11-28 21:30 +0000
  Re: .h and .cpp Paavo Helde <myfirstname@osa.pri.ee> - 2011-11-28 18:05 -0600
  Re: .h and .cpp Paavo Helde <myfirstname@osa.pri.ee> - 2011-11-28 18:27 -0600
  Re: .h and .cpp Juha Nieminen <nospam@thanks.invalid> - 2011-11-29 07:55 +0000
    Re: .h and .cpp Christopher <cpisz@austin.rr.com> - 2011-11-29 12:15 -0800
      Re: .h and .cpp Leigh Johnston <leigh@i42.co.uk> - 2011-11-29 20:19 +0000
        Re: .h and .cpp Christopher <cpisz@austin.rr.com> - 2011-11-29 12:31 -0800
          Re: .h and .cpp Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-11-30 00:31 +0000
        Re: .h and .cpp Juha Nieminen <nospam@thanks.invalid> - 2011-11-30 09:43 +0000
      Re: .h and .cpp Ian Collins <ian-news@hotmail.com> - 2011-11-30 09:23 +1300

csiph-web