If I have a piece of code, say main.cpp that requires the classes defined in myheader.h is it bad practise to then include all the libraries/headers required for main.cpp in the myheader.h file?
If so, why? Considering that main.cpp won’t work without myheader.h any way.
Sorry if this question is a little simple – I’m just unsure of the common practise with separating across multiple files.
In general, you should include only those things that are needed in the current file. OK, so
main.cppusesmyheader.hanyway, so why not include, say,<iostream>and other headers inmyheader.hwhich are needed bymain.cpp? Because tomorrow you will want to includemyheader.hintomyOthercpp.cppwhich doesn’t need<iostream>or other headers included inmyheader.h, which is redundant and increases compilation time. So, whatever is needed inmain.cpp, include inmain.cpp.There is an exception to this pattern which is called precompiled headers.