0

say I have some source code MyAmoeba.cpp, with associated header file MyAmoeba.h. Is it considered good or bad practice to include MyAmoeba.h in MyAmoeba.cpp? (I'm assuming it's never necessary, except maybe in a few badly designed cases where the header file contains a macro that makes changes to the source file)

As far as I can see, doing the above would mean that if you change the signature of anything in MyAmoeba.cpp, but forget to do it in MyAmoeba.h, then the error would happen in the compilation stage rather than in the linking stage, but I'm not sure if that's a good/bad/irrelevant thing.

I'm fairly new to c++ btw, in case this question is nonsensical in any way (please point it out to me if so)

Edit: I was playing around with a header file that contained only function declarations, but no class/struct/etc definitions. From other sources and the answers below, it seems to me that when the header file only has declarations, it's not necessary to include the header file in the associated source file, but if there are any definitions in the header file, then it is indeed necessary (because you can declare something as many times as you like, but only define it once, and also a definition can apparently be used in place of a declaration, so the function definitions in my source file would simultaneously count as my declarations).

funklute
  • 566
  • 4
  • 19
  • 5
    Actually... you always *must* include the header file (i.e. the declaration of the class(es)) from the source file where you define them.... – jsantander Jun 04 '14 at 15:22
  • @jsantander actually you could duplicate the code inside the header in the cpp file. But that would actually defy the purpose of having the header ;-) – anderas Jun 04 '14 at 15:23
  • The header file is there not only for MyAmoeba.cpp, but also for any other module that might want to talk to a MyAmoeba object. The header contains the public interface for the object. – SaganRitual Jun 04 '14 at 15:25
  • @anderas ... well, yes but then it would become completely private to the compilation unit... and couldn't be (safely) referenced from elsewhere. – jsantander Jun 04 '14 at 15:25
  • @jsantander I know, therefor the `;-)` at the end. I'm **not** advocating this at all, just playing the devil's advocate here. – anderas Jun 04 '14 at 15:28
  • Following would be a knowledge bonus: http://stackoverflow.com/questions/333889/why-have-header-files-and-cpp-files-in-c – object Jun 04 '14 at 15:32

1 Answers1

8

What?

It's almost always necessary, and thus done almost always.

If you don't have the declarations, how are you supposed to provide the definitions?

You can't just have code like

void MyAmoeba::subdivide()
{
}

without first declaring the MyAmoeba class, so the compiler can know that a method called subdivide() is indeed part of the MyAmoeba:: space.

UPDATE Please see this question for a discussion about the difference between declarations and definitions.

Community
  • 1
  • 1
unwind
  • 364,555
  • 61
  • 449
  • 578
  • 2
    And if the code in `MyAmoeba.cpp` *doesn't* need the declarations in `MyAmoeba.h` then they probably shouldn't have such similar names. – Beta Jun 04 '14 at 15:24
  • do you have time to look at this question? http://stackoverflow.com/questions/23970603/gtkmm-program-compiles-fine-but-crashes-windows-xp – Björn Hallström Jun 04 '14 at 23:18
  • Are you strictly speaking about declaring or defining the class in the header file? From other sources I've started to gather that there is a big difference between declarations and definitions, so e.g. if the header file only contains declarations, I would not need it in the source file, but if there are any definitions in the header file then I do need the header file in the source file (because of the one definition rule). But at the same time you are talking about declaring the class, not defining it, so I'm slightly confused...(see also edit to original question) – funklute Jun 05 '14 at 07:39