Subj : Re: "File Scope" objects To : comp.programming,comp.software-eng From : Gerry Quinn Date : Fri Aug 19 2005 01:19 pm In article , phlipcpp@yahoo.com says... > Gerry Quinn says... > > > >> For C++, many classes should live only inside a .cpp file. (Put another > >> way, > >> many C++ newbies think that all class definitions only belong in header > >> files, which is wrong.) > > > > You want to define class B in the implementation file of class A, i.e. > > "a.cpp". > > > > But suppose class B is written as normal, i.e. "b.h" and "b.cpp", but > > "b.h" is included only in "a.cpp". > > > > You won't be looking at "b.h" anyway except by way of finding an > > include in "a.cpp". So why worry about it? > > Because #include "b.h" is a compilable comment tempting your colleagues > "b.h" is designed to be reused. No, it's a statement that class B, used by functions of A, is declared in "b.h". If for some reason you don't want it to be re-usable except via cut-and-paste, a comment to that effect can be placed in an appropriate place. > > Only if you are sure B will be useless without A do you gain anything - > > and even then, you have a longer, more unwieldy file, in which > > functions of two classes have to be kept in order. Or you use > > something like MSVC that obscures the details of file structure by > > creating hyperlinks to functions, in which case there is also no > > benefit. > > In some situations long .cpp files speed up compiling and linking. There are > so many physical issues involved that we cannot promote the idea "put the > private class B entirely inside A's implementation file" to the level of a > "guideline". It's still a helpful suggestion, and a way to debunk the newbie > that all classes belong in .h files. You haven't presented any significant reason why it is helpful, or why all class declarations should not be in .h files. (I can think of one - a class definition so trivial it is used only in a single function.) There seems no reason whatsoever for not teaching beginners to organise ..h and .cpp files in the usual fashion, as the benefits of *ever* doing otherwise are clearly nebulous in the extreme. - Gerry Quinn .