Noob question on many .cpp or .h file.

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Farhan687
Posts: 8
Joined: August 12th, 2017, 3:17 am
Location: Pakistan

Noob question on many .cpp or .h file.

Post by Farhan687 » October 13th, 2017, 4:52 pm

What kind of side effect does it bring when i create many classes and object in my program. like a 100 .cpp and 100 .h files. will it effect the framerate of my game or there are some other complications.

Just a silly noob question. :oops: :oops: :oops: :mrgreen:

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Noob question on many .cpp or .h file.

Post by albinopapa » October 13th, 2017, 5:27 pm

Framerate should not be affected, however, compile time might be lengthened depending on how many of those cpp files need to be compiled.

Your header files (.h) are included in various .h or .cpp files. The compiler will look through each header file that is included. If you don't #include all 100 files in your project, the corresponding .cpp files that you didn't #include their headers, won't be compiled.

If all 100 headers are included and you use a class or function from each one, then it will include the headers and compile all 100 corresponding cpp file. This is why some people choose to forward declare classes or structs in the headers and #include headers only in cpp files when you can. By using this strategy, you can reduce the number of copies of headers to cpp source files. This helps reduce recompile times when something in a header file changes. The reason this works is because when you #include a header in say another header, and that header is #included in yet another header the first header file is also copied in to the last source file.

Code: Select all

FileA.h
class SomeHelperClass{};

FileB.h
#include "FileA.h"
class SomeClass{};

FileC.h
#include "FileB.h"
// Now FileC.cpp has a copy of FileB.h and FileA.h even if FileC.cpp didn't need to know anything about what's in FileA.h.
If the #includes had been in the .cpp files, FileC.cpp would only get a copy of FileB.h which means shorter compile times and possibly smaller compiled program sizes.

Other than that, it shouldn't affect runtime performance having thousands of .h/.cpp files in a project. In the end, the compiler will only compile what it needs and try to optimize out as much as possible.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply