0

I have a very simple class and I'd like to consolidate it to a single .h file. Will everything work the same if I cut and paste the guts of my .cpp to the bottom of my .h?

In particular, there are static member variable initializations int MyClass::myStaticVar = 0; outside of any class definition at the top of the .cpp, and following that, there are static member function implementations void MyClass::myStaticMethod() {...}. Some non-static member functions are already being implemented in the .h, not the .cpp. So you can see there are some nuances here that I'd like to clarify.


Edit So far, what I'm getting is:

This is naughty, but it will work if you only #include the .h once. It breaks the convention and doesn't really work like a .h so it might as well be named .doofus.

Now, for example, look at the TUIO C++ bindings. A lot of the classes consist of one .h file, no cpp (TuioPoint.h, TuioCursor.h, TuioObject.h, etc). I don't think this is so bad...

Matt Montag
  • 7,105
  • 8
  • 41
  • 47
  • Hard to say.. we'd have to see code. Why would you want to do that? –  Sep 27 '11 at 19:31
  • Dragons may come out of your nose. – JRL Sep 27 '11 at 19:31
  • You might go so far as to... rename the file to '.hpp' ... – phooji Sep 27 '11 at 19:32
  • 1
    @JRL: I can't think of how this leads to undefined behavior if that's what your referencing. – Mooing Duck Sep 27 '11 at 19:43
  • @Matt: Even having real code examples, this is definitely bad practice. This question may help understanding http://stackoverflow.com/q/583255/198011 – Antonio Pérez Sep 27 '11 at 21:09
  • Thanks Antonio, I have seen that question before, but definitely good to review all this and have it down concrete. I do play devil's advocate in order to get a complete explanation sometimes. – Matt Montag Sep 27 '11 at 22:25

4 Answers4

5

If you're left with a single cpp file in the entire project, then it will work (but it's bad practice). If you have two cpp files that both include that header, you're breaking the one definition rule, and you (should) get linker errors.

You can do this if (A) All the functions are templates (in fact, you must in this case), or (B) all the functions are marked as inline.

[Edit] The reason you aren't already having problems is a function defined in the class definition is automatically marked as inline. Thus: no problems. However, if the function is defined outside of the class definition, it should be in a cpp file. Also, static members should always be in a cpp file.

[Edit2] The reason non-inline, non-template functions and File scope varaibles (globals and static members) should always be in a cpp file, is that when the compiler finds that line of code, it creates the function/variable right there. Obviously, it must be created once to be used. Why not in a header file? Because then if the header is included in two cpp files, it will be created in two places (I have hpp files at work that are literally included in several thousand cpp files). C++ has a "one-definition rule" where each function/object can only be defined/created once, to prevent this obvious error:

int MyClass::myStaticVar = 0;
int MyClass::myStaticVar = 7;

Which would it use? You've just created two variables with the same name! So this isn't allowed, even if they were exactly the same, (except for inline/template). Each cpp file is compiled once and only once (unless for some oddball reason it's included from something else), which prevents accidental violations of the one-definition rule.
Also, hpp files are for declarations, and cpp files are for definitions/instantiations.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
1

what good would the .h file be anymore? you can't have multiple .cpp files #include this .h file. And if this .h is only included by a single .cpp, then why do you need the .h file in the first place - just put everything in the .cpp.

Jay
  • 435
  • 3
  • 5
  • Why do I need the .h in the first place? Because it's going to be reused in multiple projects, but not more than once in each project. – Matt Montag Sep 27 '11 at 20:09
1

In addition to Mooings answer you might want to consider the compile/link process for a while.

  • The compiler compiles the .cpp files, not the .h files (by convention).

This has the consequence that for each .cpp file you need the definitions for the classes you reference in order to create instructions for the code in the .cpp. The .h files provides that.

What you do not want is identical pieces of code being duplicated across your program, which would be the consequence of compiling .cpp files including headers with implementations(what you are suggesting); hence the one definition rule.

In a one .cpp-file project as Mooing suggests you can of course abuse this to your delight as long as you have a .cpp with a main and only one set of includes.

Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
0

Essentially, this is what #include does (it pastes the header into the cpp, basically doing the same thing in a sense). So, yes, everything SHOULD work out fine, assuming no odd cases. I can't see why you would want to do this though. You'd be better off just flat out defining the functions and such in the class definition or just using #include. Is there any reason you're choosing not to?

EDIT: In response to your edits, why are you implementing members in the header? I'd suggest moving those to the .cpp unless this is a template class or some similar special case. Use the header for prototype and decleration, use the cpp for definition. That should solve any issues for you.

MGZero
  • 5,812
  • 5
  • 29
  • 46
  • Is it customary to `#include` .cpp files? Genuine question, not a C++ programmer... – Pascal Cuoq Sep 27 '11 at 19:32
  • No, include generally takes the .h file and places it in the .cpp file. With this technique, you might have issues if you #include the .h file in more than one .cpp file. – Eclipse Sep 27 '11 at 19:33
  • I have added some details, should explain where I'm coming from with this – Matt Montag Sep 27 '11 at 19:34
  • @Pascal Cuoq I've seen it, but it's not common at all. – MGZero Sep 27 '11 at 19:41
  • @PascalCuoq: I do occassionally `#include` a `cpp` file for definitions of `template` classes/functions. `template` functions must be in included in the header, but I like to keep declaration/definition separate. – Mooing Duck Sep 27 '11 at 19:42
  • -1 The OP asks to paste the `.cpp` code into the `.h`, not the other way around. – Antonio Pérez Sep 27 '11 at 20:05
  • @Antonio Which logically is the same thing, assuming only one file is used, hence my mentioning of "odd cases." – MGZero Sep 27 '11 at 20:13
  • @Antonio I just realized that still sounds confusing. What I mean by the same thing is if I do #include header *function implementations* or *class definition* #include cpp, I'm going to end up with the same file after the preprocessor runs. – MGZero Sep 27 '11 at 20:20
  • @MGZero I realize you end up having the same result, i.e. everything in one file, but your answer is still confusing, since it doesn't address the issue of including such a file into different translation units. Other answers do. – Antonio Pérez Sep 27 '11 at 21:05
  • @Antonio Alright, I'll take that as a legit reason for a downvote! :) – MGZero Sep 27 '11 at 22:58