C++ Tips: Difference between revisions

Jump to navigation Jump to search
313 bytes added ,  7 October 2009
No edit summary
Line 28: Line 28:


* Prefer forward declaration to includes. Some things require pulling in a header, but in general headers should pull in everything the need to compile independently, and nothing more. Passing aggregate types by reference helps, since then a forward declaration suffices to refer to them. Avoid pulling in standard library headers (or system headers) into if you can avoid it, and never pull <tt>iostream</tt> into a header - use <tt>iosfwd</tt>, which exists for the purpose.
* Prefer forward declaration to includes. Some things require pulling in a header, but in general headers should pull in everything the need to compile independently, and nothing more. Passing aggregate types by reference helps, since then a forward declaration suffices to refer to them. Avoid pulling in standard library headers (or system headers) into if you can avoid it, and never pull <tt>iostream</tt> into a header - use <tt>iosfwd</tt>, which exists for the purpose.
* The compiler is almost certainly smarter about inlining than you are. One line methods can live in header files, sometimes. Anything more should live in an implementation file. Otherwise, compile times increase for each compilation unit that includes the header. (Obviously templates are an exception to this)


== Pitfalls ==
== Pitfalls ==
580

edits

Navigation menu