[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: when...
On Fri, 18 Jun 1999, Christian Reiniger wrote:
> namespace XYZ
> {
> char *Hello = "World";
> bool AmIStupid (void);
> int Everything = 42;
> };
>
> Here the two vars and the function are in the "XYZ" namespace. That means
> if you want to access them from the "outside", you have to prepend "XYZ::"
> (e.g. int Answer = XYZ::Everything;).
> If you want to access them from within the namespace (e.g. if you're in the
> body of function AmIStupid () and want to access Hello) you don't need that.
Also worth mentioning the 'using' directive:
If you had the above, instead of
int Answer = XYZ::Everything
you could use, either:
using XYZ::Everything;
int Answer = Everything;
or
using XYZ;
int Answer = Everything;
You might also noticed that the new *.h header files, now have the
'using std;' directive.
That is because all all standard library functions are now in 'std'
namespace.
This might be of interest as well:
>From clarke@ses.com Sat Jun 19 11:18:20 1999
Date: Mon, 17 May 1999 15:23:49 -0500 (CDT)
From: "Allan D. Clarke" <clarke@ses.com>
To: cpptips@ses.com
Subject: c++ (nested classes versus namespaces)
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() { }
...
--
Nicholas