Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including header in the class header and implementation?

Tags:

c++

I have a class Foo with a corresponding .h and .cpp. Both files make use of an std::string. Should I only #include <string> in the header or include it in both files?

like image 411
flumpb Avatar asked Mar 12 '26 23:03

flumpb


1 Answers

if the header can get away with a forward declaration then I always use that in the header and the #include in the implementation file. (FD can be used for reference and pointer types).

If they both need it, then i tend to put them in both so that if the header is changed I can remove it from the .h.

like image 56
111111 Avatar answered Mar 14 '26 12:03

111111



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!