Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 14th, 2006, 5:14 AM   #11
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
The original question was not that clear, Jimbo.

Some people, when they have nothing to say, point out the irrelevant, eh tempest?
grumpy is offline   Reply With Quote
Old Apr 14th, 2006, 5:47 AM   #12
biohazard
Newbie
 
biohazard's Avatar
 
Join Date: Feb 2006
Posts: 18
Rep Power: 0 biohazard is on a distinguished road
It might appear completely unrelated to you Grumpy, but I am new to namespaces. So I was wondering why you did not use namespace std. Now I got the point. Using std:: exempts one from bringingwhole of namespace into the scope.
biohazard is offline   Reply With Quote
Old Apr 14th, 2006, 6:15 AM   #13
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Quote:
Originally Posted by biohazard
It might appear completely unrelated to you Grumpy, but I am new to namespaces.
I didn't say your question was unrelated. I simply referred back to my previous post in the process of answering.
Quote:
Originally Posted by biohazard
So I was wondering why you did not use namespace std. Now I got the point. Using std:: exempts one from bringingwhole of namespace into the scope.
What you describe here is the reverse of reality.

A "using namespace" directive does not bring a namespace into scope. It actually tells the compiler that, if it encounters a name, that it should look within the namespace for candidates that match that name.

A "using namespace std;" directive potentially exempts one from having to use the "std::" prefix to fully qualify a name (not the reverse, which you stated). I say potentially, because it can also introduces ambiguity. For example;
#include <iostream>

using namespace std;

namespace X
{
    int cout;
};

using namespace X;

int main()
{
    cout << "Hello\n";
}
will result in a compile error, as there are two candidates named cout (std::cout and X::cout) that are candidates to match the name used in main(), and the compiler has no reason to prefer either one over the other. "using namespace" directives cannot be switched off after the fact, so the only way to eliminate the ambiguity is to change the main() function to this;
int main()
{
    std::cout << "Hello\n";
}
grumpy is offline   Reply With Quote
Old Apr 14th, 2006, 6:43 AM   #14
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
Quote:
Originally Posted by InfoGeek
Also I'd bring the whole of the std namespace into scope.
Quote:
Originally Posted by grumpy
A "using namespace" directive does not bring a namespace into scope. It actually tells the compiler that, if it encounters a name, that it should look within the namespace for candidates that match that name.
I stand corrected.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Apr 14th, 2006, 7:14 AM   #15
biohazard
Newbie
 
biohazard's Avatar
 
Join Date: Feb 2006
Posts: 18
Rep Power: 0 biohazard is on a distinguished road
using namespace std;

namespace X
{
    int cout;
};


What is namespace X for, as we are already using namespace std?Is it a namespace in a namespace?
biohazard is offline   Reply With Quote
Old Apr 14th, 2006, 7:19 AM   #16
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
It's a user-defined namespace.

EDIT: you may like to read about namespaces here(first google hit).
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Apr 14th, 2006, 7:46 AM   #17
biohazard
Newbie
 
biohazard's Avatar
 
Join Date: Feb 2006
Posts: 18
Rep Power: 0 biohazard is on a distinguished road
Thanx InfoGeek. I got a basic idea of namespaces.
biohazard is offline   Reply With Quote
Old Apr 14th, 2006, 7:47 AM   #18
biohazard
Newbie
 
biohazard's Avatar
 
Join Date: Feb 2006
Posts: 18
Rep Power: 0 biohazard is on a distinguished road
Correction: I meant using namespaces. Sorry.
biohazard is offline   Reply With Quote
Old Apr 14th, 2006, 10:53 AM   #19
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
namespace X was a namespace I used in the example to illustrate one of the problems that can be cased by "using namespace" directives. The purpose of namespaces is to allow a program to use multiple things with the same name (eg cout in my example), as long as the things with the same name are placed into different namespaces. A using directive can work against that purpose (and confuse the compiler) if not used carefully.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:20 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC