Re: Elegant way to do this?
From: Anthony Borla (ajborla_at_bigpond.com)
Date: 12/11/04
- Next message: Anthony Borla: "Re: HashTable"
- Previous message: Anthony Borla: "Re: Polymorphism"
- In reply to: Rich: "Re: Elegant way to do this?"
- Next in thread: Francis Glassborow: "Re: Elegant way to do this?"
- Reply: Francis Glassborow: "Re: Elegant way to do this?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 11 Dec 2004 12:23:56 GMT
"Rich" <Someone@somewhere.com> wrote in message
news:cpeknq$41s$1@titan.btinternet.com...
>
<SNIP>
>
> One quick question
> I notice from a lot of code examples posted here that using
> namespace std is rarely if ever used and instead cout or
> cin or wahtever is preceeded by a namespace resolution
> operator ::
>
> This seems like more work than using the using statement.
>
> I would imagine that there is a very good reason for the std:: use
> rather than a using statement, or is it just a matter of preference?
>
To answer your question about why this is done:
using std::cout;
using std::cin;
...
rather than this:
using namespace std;
In a nutshell, to reduce the possibility of name clashes occuring by
exposing only the names that actually *need* to be exposed in order for the
code to compile. It is for this very purpose that the namespace facility was
introduced. To then expose all names within the namespace, which is what
this:
using namespace std;
does, pretty well makes the namespace facility's presence redundant.
A simple name clash example. I am creating an enumeration:
enum Months { jan, feb, ..., oct, nov, dec };
and have exposed the entire 'std' namespace. Will my code compile ? No,
because two names used in the enumeration, namely:
oct
and:
dec
also occur in the 'std' namespace. The compilation error was due to a
totally unnecessary [not to mention bothersome and time wasting to find and
fix] name clash, something which would not have occured had I only exposed
only the names actually needed.
I daresay this practice is seen in a lot of introductory text book code
probably to eliminate distracting elements thus keep the code compact and
help the reader remain focussed on the more important aspects of the code.
Whilst this may be beneficial, the downside is that it can help perpetuate a
rather poor practice.
I hope this helps.
Anthony Borla
P.S.
There is much more to namespaces than has been discussed, so I would
recommend you do more relevant reading
- Next message: Anthony Borla: "Re: HashTable"
- Previous message: Anthony Borla: "Re: Polymorphism"
- In reply to: Rich: "Re: Elegant way to do this?"
- Next in thread: Francis Glassborow: "Re: Elegant way to do this?"
- Reply: Francis Glassborow: "Re: Elegant way to do this?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|