tags:

views:

160

answers:

7

Is there anything wrong with having a single class (one .h) implemented over multiple source files? I realize that this could be a symptom of too much code in a single class, but is there anything technically wrong with it?

For instance:

Foo.h

class Foo
{
   void Read();
   void Write();
   void Run();
}

Foo.Read.cpp

#include "Foo.h"
void Foo::Read()
{
}

Foo.Write.cpp

#include "Foo.h"
void Foo::Write()
{
}

Foo.Run.cpp

#include "Foo.h"
void Foo::Run()
{
}
+4  A: 

Nope, nothing technically wrong with it. The linker will bring all the pieces of Foo together in the final binary.

fbrereto
+8  A: 

This is fine. In the end, it will be all linked together.

I have even seen code, where every member function was in a different *.cpp file.

drhirsch
That would drive me batty. I'm sure there are occasionally good (or at least decent) reasons to do this, but from the perspective of trying to understand the object model, bleah... to have to jump files to follow methods around.
Joe
If you build a library, splitting every function and global into its own object allows the consumer to link only the bits they reference and no more. It's annoying to work with, though.
ephemient
+1  A: 

I've been working with the Apache Portable Runtime, which does pretty much this exact thing. You have a header, say apr_client.h and multiple implementation files for the functions in that header -- each file representing one aspect of client operations. It's not wrong, and it's not really unusual.

this could be a symptom of too much code in a single class

C++ is not Java, so you don't have to pick your file names according to your class names.

Max Lybbert
A: 

yea thats legitmate, and in fact newer languages like C# do that sort of thing of all the time.

Nate Gates
+4  A: 

That is legitimate and it has some (!?) advantages...

If you link your executable with the static library of this class, only the used functions will get in. This is very handy for limited-resource systems.

You can also hide implementation details of certain functions. Two people may implement parts of a class without knowing about each other. Handy for DOD projects.

If you look at any CRT source, you will see the same pattern...

Malkocoglu
The point re hiding implementation details for the same class is really stretching it. If you have a class which can be split so easily, then it should be 2 classes not one.
Richard Corden
@Richard: Well, I just wanted to say that you can do it, you do not have to do it. If the function is so simple, then you may not prefer to create a separate class for it...
Malkocoglu
@Richard - eh? - what about a "device" for instance foo::read foo::write foo::ioctl what about the benefits of shorter build times?
pgast
+1  A: 

It is perfectly valid. All cpp files will be linked together.

This can be useful for, as you said, making a very large implementation file more readable and, since each cpp file is a compilation unit, you could (ab)use that fact. (Unnamed namespaces for example)

Seth Illgard
A: 

There are two points which may be worth considering.

Class wide optimisations:

If the compiler has all of the member functions visible to it when its parsing the file (assuming a single TU compiler) then it may be able to perform optimisations between the different member functions.

Code Review

If all the definitions for a class are in the same TU, then this makes manual code review easier. If the definitions are split into different TUs, this significantly increases the effort required for a manual review as the appropriate files need to be searched for, with constant flicking between windows to get the "whole" picture.

Richard Corden