Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Sep 26th, 2005, 5:24 AM   #1
aniruddha_dhondge
Newbie
 
Join Date: Sep 2005
Location: Mumbai (Bombay)
Posts: 9
Rep Power: 0 aniruddha_dhondge is on a distinguished road
Regarding namespace

What is the use of "using namespace std"?
How the compiler gets the function prototype(signature)?


eg.

#include <iostream> // Dosen't look in iostream.h
using namespace std;

int void main()
{
cout<<"\n Enter first number:="; //How the compiler get the
//object "cout"
}

Can anybody explain sequence of finding "cout" object and where it is created ?
aniruddha_dhondge is offline   Reply With Quote
Old Sep 26th, 2005, 7:37 AM   #2
jonyzz
Programmer
 
jonyzz's Avatar
 
Join Date: Aug 2005
Location: null
Posts: 40
Rep Power: 0 jonyzz is on a distinguished road
This is an interesting question you have posted aniruddha_dhondge.
This is something that I would also like to know.
I won't be able to answer to your question but I will post some assumptions, hoping that any of you guys will correct me if I am wrong.
I know that by including the 'iostream' file you perform an action that is similar to pasting its contents at the top of your code. But when specifying 'using namespace std' is it that you are 'pasting' only the part defined in 'std' ? I don't know.
The 'cout' object must be defined in <iostream> - in the namespace 'std'. Sorry to admit that, but I haven't seen how the source of the file looks, so I am just guessing.
I also don't know what is the difference between <iostream> and <iostream.h> Maybe they are different files with the same name but in different folders.
I am not very familiar with namespaces, but if you don't specify the 'using namespace std' your 'cout' object might not be recognized. I suppose 'cout' is searched in the namespace 'std' and the namespace 'std' is searched in any of the files included (I might not be right). You could call it also this way 'std::cout' if you don't want to specify the whole namespace.
When exactly is the object created? I suppose it is declared somewhere in <iostream> and thereby is created when the program is started by the time the compiler reaches the line with its declaration.
jonyzz is offline   Reply With Quote
Old Sep 26th, 2005, 7:47 AM   #3
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
At the least, I know that std is the namespace for the standard C++ libraries, while if you used System, then your app would be using Managed Extensions for C++.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Sep 26th, 2005, 7:50 AM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Please use code tags around code (refer to the "how to post" post for information on how to sensibly ask questions). And take out the keyword void between "int" and "main"

That aside, the answer to your question follows.

std::cout is a global object declared in <iostream>; that name "std::cout" means it has a basic name of cout, and lives within the std namespace.

Without the "using namespace std;" directive, when a compiler encounters a name, it will look in the current scope for something which matches that name. In the case of cout, it is unable to find anything that has the name cout (as it does not look in namespace std). The result will be a compiler error about an invalid identifier (the actual wording of any error message is compiler dependent).

The "using namespace std;" directive instructs the compiler to match names to objects (or other entities) differently. It is literally a directive that anything in namespace std is a candidate when trying to resolve a name. So, when it encounters the name "cout", std::cout is a candidate which will now match that name. In your example, it is the only candidate, so the the compiler resolves "cout" to "std::cout".

Things would get a little bit trickier if you have another namespace with a cout object, and a using namespace directive for that namespace. For example;
#include <iostream>
using namespace std;
namespace X
{
    int cout;
}
using namespace X;

int main()
{
    cout << "Hello\n";
}
In this case, the compiler will complain, but for a different reason. When it sees the name "cout", it finds there are two possible candidates (X::cout and std::cout). It does not have the intelligence to resolve the difference, so complains about an ambiguity.
grumpy is offline   Reply With Quote
Old Sep 26th, 2005, 7:53 AM   #5
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
I think the difference between iostream and iostream.h is that when you specify #include<iostream.h>, it refers to the file iostream.h and the file must be present at the include path. But specifying #include<iostream> doesn't necessarily require the file but it just requires the compiler to declare whatever declarations and definitions are meant to be in the iostream header as required by the standard.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Sep 26th, 2005, 8:10 AM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by jonyzz
I won't be able to answer to your question but I will post some assumptions, hoping that any of you guys will correct me if I am wrong.
OK; consider yourself corrected when you are wrong
Quote:
Originally Posted by jonyzz
I know that by including the 'iostream' file you perform an action that is similar to pasting its contents at the top of your code.
Correct, except that the words "is similar" would be better stated as "has an identical effect".

#include <file> literally pastes the contents of file in place of the #include directive.

This is done by the preprocessor asa straight text substitution before the compiler attempts to interpret the code.
Quote:
Originally Posted by jonyzz
But when specifying 'using namespace std' is it that you are 'pasting' only the part defined in 'std' ? I don't know.
No. "using namespace std;" has no relationship to the preprocessor. It does not change how the preprocessor works in any way.

Refer to my previous post for what the "using namespace" directive does.
Quote:
Originally Posted by jonyzz
The 'cout' object must be defined in <iostream> - in the namespace 'std'.
An object named cout, which lives in the namespace std, is declared in the <iostream> header. It might be defined (i.e. fully implemented) in that header, but it might not.

The distinction I'm making is analogy is that
int Function();
which might appear in a header declares a function but does not define it. The definition of a function typically lives in another source file, that might do this ...
#include "the-header"
int Function()
{
    return 42;
}
This is the definition. It may seem that I'm splitting hairs by distinguishing between declaration and definitions in this way. But the C and C++ standards make this sort of distinction, so it is a good idea to try to understand them.
Quote:
Originally Posted by jonyzz
I also don't know what is the difference between <iostream> and <iostream.h> Maybe they are different files with the same name but in different folders.
<iostream.h> was a header that was part of draft C++ standards, before someone on the standardisation commitee came up with the notion of requiring the C++ standard headers to not have a ".h" extension and for the contents of those headers to live in namespace std.

<iostream.h> is non-standard, but is supported by some older compilers which were created while the C++ standard was in draft form. It is still supported by some modern compilers (and their libraries) because there was code written using those pre-standard headers, and the compiler vendors continued to support the feature - in the interests of not unnecessarily annoying customers of older versions of their compiler.

Quote:
Originally Posted by jonyzz
I am not very familiar with namespaces, but if you don't specify the 'using namespace std' your 'cout' object might not be recognized. I suppose 'cout' is searched in the namespace 'std' and the namespace 'std' is searched in any of the files included (I might not be right). You could call it also this way 'std::cout' if you don't want to specify the whole namespace.
Look at my previous post for what the using directive does. In the example in the original post, the compiler will not be able to resolve "cout" to "std::cout".
Quote:
Originally Posted by jonyzz
When exactly is the object created? I suppose it is declared somewhere in <iostream> and thereby is created when the program is started by the time the compiler reaches the line with its declaration.
The basic stream objects (std::cout, std::cin, ctd::cerr, and a couple of others) are created before main() is called --- and also before the constructors of any user-defined global objects are invoked. In other words, they are created by compiler-specific startup code.
grumpy is offline   Reply With Quote
Old Sep 26th, 2005, 8:15 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
The compiler doesn't know what declarations are in iostream without the file. iostream is a file. iostream.h is the old deprecated version. It should not be used.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 26th, 2005, 9:17 AM   #8
jonyzz
Programmer
 
jonyzz's Avatar
 
Join Date: Aug 2005
Location: null
Posts: 40
Rep Power: 0 jonyzz is on a distinguished road
Thanks for the answers.

@grumpy: you really posted a very detailed explanation. Thanks But did I understand right that I cannot use the following, because cout won't be recognized ?

#include <iostream> 

void main()
{
std::cout<<"Hello";
}
jonyzz is offline   Reply With Quote
Old Sep 26th, 2005, 10:04 AM   #9
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 jonyzz
But did I understand right that I cannot use the following, because cout won't be recognized ?

#include <iostream> 

void main()
{
std::cout<<"Hello";
}
No, you didn't understand right. cout will be recognized as the namespace is specified. Above code will work.

what grumpy was saying was that if you have two namespaces both having the cout object and both were included in the global namespace by using namespace directive, then compiler won't be able to resolve which cout you're referring to.
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Sep 26th, 2005, 1:26 PM   #10
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5 grumpy is on a distinguished road
Quote:
Originally Posted by jonyzz
But did I understand right that I cannot use the following, because cout won't be recognized ?

#include <iostream> 

void main()
{
std::cout<<"Hello";
}
No, you don't understand.

The basic rule is that, when it sees a name, the compiler must resolve that name to something specific (eg a type, an object [as in this case]). In this case, std::cout is a candidate object that matches the fully qualified name std::cout, so the using directive is not necessary.

The using directive is needed when the name of the object is not fully specified (eg cout is specified, when the intent is to use std::cout).

The example I gave illustrates what happens when two "using namespace" directives are in force, and an object of the same name exists in both namespaces.

Although unrelated to namespaces, main() should return int.
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 7:31 PM.

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