C++ Tips: Difference between revisions

Jump to navigation Jump to search
478 bytes added ,  7 October 2009
Line 18: Line 18:


* Always pass aggregate types by (const) reference, especially STL containers, <tt>string</tt> or mathematical tuples such as <tt>SGQuat</tt> or <tt>SGVec3</tt>. String copying in particular is common and leads to many wasteful <tt>malloc</tt> and <tt>free</tt> calls. Passing STL containers by value is extremely wasteful, as is returning them by value - pass them into methods by a reference, and out the same way. This avoids any container copying at all, hopefully.
* Always pass aggregate types by (const) reference, especially STL containers, <tt>string</tt> or mathematical tuples such as <tt>SGQuat</tt> or <tt>SGVec3</tt>. String copying in particular is common and leads to many wasteful <tt>malloc</tt> and <tt>free</tt> calls. Passing STL containers by value is extremely wasteful, as is returning them by value - pass them into methods by a reference, and out the same way. This avoids any container copying at all, hopefully.
* 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.


== Pitfalls ==
== Pitfalls ==
580

edits

Navigation menu