Bruce Eckel's Thinking in C++, 2nd Ed | Contents | Prev | Next |
The other reason you’ll want to nest a class is as part of the private implementation. Here, nesting is beneficial for implementation hiding rather than class association and the prevention of namespace pollution as above.
Derived(const Derived& d) : base(d) { // ...
class ostream;
This approach leaves your code vulnerable to changes in representation. (For example, ostream could actually be a typedef.) Instead, always use the header file:
#include <iostream>
When creating your own classes, if a library is big, provide your users an abbreviated form of the header file with incomplete type specifications (that is, class name declarations) for cases where they only need to use pointers. (It can speed compilations.)
return MyType(i, j);
rather than
MyType x(i, j);
return x;
The former return statement eliminates a copy-constructor call and destructor call.