TITLE: nested classes versus namespaces

(Newsgroups: comp.lang.c++, 10 May 99)


NIGHTSTRIKE: NightStrike <nightstrike@home.com>

...


NARAN: Siemel Naran <sbnaran@localhost.localdomain>

There's no real disadvantage to making a class nested.  Here are some
issues to consider.

It takes more typing to created an instance of a nested class.  Eg,
   Section s;
   Remind::Section s;
With namespaces
   namespace Remind { class Section { ... }; }
   int main() { using Remind::Section; Section s1; Section s2; }

You can't forward declare nested classes,
   class Remind::Section; // illegal
   void f(Remind::Section *);
But you can forward declare namespace classes,
   class Section; // fine
   void f(Section *); // fine

If you decide to add more nested classes, too bad.  The enclosing
class can be reopened.  This is probably the biggest disadvantage
with nested classes.
   // file1.h
   struct Remind { class Section1 { ... }; };
   // file2.h
   struct Remind { class Section2 { ... }; };
   // main.c
   #include "file1.h"
   #include "file2.h" /* error: redefinition of class Remind */
But namespaces can be reopened.

Nested classes are good for private data.  Eg,
   // X.h
   class X { class Imp; std::auto_ptr<Imp> imp; public: ... };
   // X.c
   #include "X.h"
   struct X::Imp { ... };
   X::~X() { }
   ...

