1

Sometimes I just need .h but not .cpp file, eg : interface class or enum class, and when the class list is displayed on file list of IDE (eg:Xcode):

Animal.cpp
Animal.h
Cat.cpp
Cat.h
Dog.cpp
Dog.h
IEnumerable.h
Lizard.cpp
Lizard.h
MyEnum.h
Penguin.cpp
Penguin.h

I felt uncomfortable to see this file list because IEnumerable and MyEnum don't have their own .cpp file, and I feel the file list will be more tidy if every .h is associated with a .cpp:

Animal.cpp
Animal.h
Cat.cpp
Cat.h
Dog.cpp
Dog.h
IEnumerable.cpp
IEnumerable.h
Lizard.cpp
Lizard.h
MyEnum.cpp
MyEnum.h
Penguin.cpp
Penguin.h

So my question is, is the readability of file list in IDE a reason to keep empty .cpp file even if when I don't need it?

  • 2
    One good reason to have .cpps for everything is so you can include the header and guarantee that the compiler sees it at least once during the build. – Blrfl Aug 16 '17 at 3:31
  • 1
    If it helps you, I don't see why not. – GrandmasterB Aug 16 '17 at 3:41
  • 1
    Just like code should be written with readability (not just by you, by others as well) in mind, same goes for project structure. A bunch of empty source files could be considered confusing. It's not because it somehow looks more symmetrical to you, in your particular IDE, that it's the case for others. Also to me it makes no sense at all to have a source file for interfaces or enums. The compiler should see them anyway unless they're not used. Even then it would arguably be better to write a test for them instead of just opting for compiler visibility. – stijn Aug 16 '17 at 7:40
  • Many libraries are 99% header files (Boost for example). If you had hundreds of header files and only needed one cpp file you wouldn't make hundreds of empty cpp files. But if it is just a couple then the person that downloads your project will think that the download failed to get those files properly. So make sure you put a comment in the cpp file that says "intentionally blank" – Jerry Jeremiah Sep 14 '17 at 2:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.