If I do not separate a class declaration and definition, but put my class definition in a header file, then will this definition code get re-compiled for every source file that includes that header?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s compiled once for every translation unit (from which an object file is then built.) Unless you’re using some form of pre-compiled header functionality in your compiler.
The multiple inclusion guards you normally put in headers are there to avoid multiple compilation in the same translation unit only. Unless the compiled result of the header has been stored somewhere by the compiler’s pre-compiled header mechanism, the header will need to be compiled again in the next translation unit.
This is one of the reasons why C++ code tends to compile much slower than C code, and why it’s important to make proper use of forward declarations and avoid unneeded
#includestatements. I’ve seen cases where build times were cut in half just by using forward declarations instead of including the whole header.