![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Sep 2005
Location: Mumbai (Bombay)
Posts: 9
Rep Power: 0
![]() |
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 ? |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Aug 2005
Location: null
Posts: 40
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#3 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
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";
} |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#6 | |||||||
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Quote:
![]() Quote:
#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:
Refer to my previous post for what the "using namespace" directive does. Quote:
The distinction I'm making is analogy is that int Function(); #include "the-header"
int Function()
{
return 42;
}Quote:
<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:
Quote:
|
|||||||
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#8 |
|
Programmer
Join Date: Aug 2005
Location: null
Posts: 40
Rep Power: 0
![]() |
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";
} |
|
|
|
|
|
#9 | |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
Quote:
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. |
|
|
|
|
|
|
#10 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,223
Rep Power: 5
![]() |
Quote:
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. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|