namespace question

This is a discussion on namespace question within the C++ Programming forums, part of the General Programming Boards category; Hi,all~ when I read my book, something seems confused to me. the code is here: #include <iostream> #include <cstdlib> void ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Question namespace question

    Hi,all~

    when I read my book, something seems confused to me. the code is here:

    #include <iostream>
    #include <cstdlib>

    void main(){
    int a=0;
    int b;
    b=std::abs(a); // is it required ?
    cout << b << endl;
    }

    it works fine but it also works fine when I get ride of std::, then what it act here ???
    Last edited by black; 06-02-2002 at 10:01 PM.
    Never end on learning~

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    317
    Is that the entire code that you used or did you need to include iostream as well (for cout and endl and <<)? My guess is that you had to use iostream which, if your program worked, brought the namespace into scope.
    Last edited by Traveller; 06-02-2002 at 09:11 PM.

  3. #3
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Thumbs up

    Originally posted by Traveller
    Is that the entire code that you used or did you need to include iostream as well (for cout and endl and <<)? My guess is that you had to use iostream which, if your program worked, brought the namespace into scope.
    sorry for omitting that, thanx.

    And... any ideas on my questions ?
    Never end on learning~

  4. #4
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Hi Black,

    That has to do with namespaces. iostream.h is part of the standard library. this library is itself enapsulated under a namespace... the std namespace (find info for the namespace keyword, if you don't know what it is).

    Now, what happens is that ANSI/ISO C++ asks you to either qualify those commands that are part of the standard library or to bring the namespace into the global scope.

    That code of yours shows you the first option. The :: is the qualifying operator and it basically is telling the compiler "I want to use cout from the standard library (std)and not some other cout that you mat have around."

    The second option is when you write a line like the following:

    using namespace std;

    This tells the compiler, "from now on search for every name you see in the standard library first. If you find it, that's the one I want." This way you avoid using the qualifying operator. But it does have some disadvantages and is not that good of a practice to bring a namespace into global scope.

    So, by now you should be asking "that's fine and dandy, but I have no 'using namespace std;' on my code and yet when I remove the 'std::' it still works!"

    Yeah, you're right. That's, as I see it, another all users are stupid(TM) from some compiler developing companies. What happens is that they changed the header files a bit and are indeed calling "using namespace std;" from them.

    Open your iostream.h file and take a look at it's code... it's not that big. Now, you'll see a piece of code, probably at the end, that reads something like the following:

    #ifndef __USING_STD_NAMES__
    using namespace std;
    #endif

    __USING_STD_NAMES__ depends on the compiler, so for yours it may be some differente name. But basically, what it means is that if your compiler is set to use std names by default, the header file alone will already call it into global scope. And that's why it also works if you remove "std::" from your code even when you don't have "using namespace std;". Because, it is in fact there, hidden in the header file.

    This also means your compiler should have an option to turn this auto-using std namespace off. Something I strongly advise as soon as you feel comfortable with the standard library and/or start developing projects with your own namespaces.
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    thanx Mario. your words must be helpful on my way learning C++.
    Never end on learning~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 05:22 AM
  2. namespace question
    By Dave++ in forum C++ Programming
    Replies: 6
    Last Post: 06-04-2007, 06:47 PM
  3. A fourth noobie question - namespace std?
    By Noobie in forum C++ Programming
    Replies: 24
    Last Post: 08-12-2005, 03:10 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. using namespace std; question.
    By fuh in forum C++ Programming
    Replies: 2
    Last Post: 12-30-2002, 04:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21