Sign in to follow this  

C++: namespaces etc.

This topic is 4360 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey! I have a few questions I would really appreciate if someone answered. :) I'm reading a book and they might save it for later but I'm curious and I might forget the questions later :P If I'm using namespace std, is that like typing std:: in front of everything? Not quite? What does ::something mean? Do I go outside the std namespace then (if I'm using it)? Or just outside whatever class I'm currently in? If I make a function in a source file that is using namespace std, will that function be in namespace std or some place else? Why? I've run some tests to partialy answer the questions but I know by now that there might be more to it than the eye can see.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yey, this is fun :)

Ok, the operator :: (you'll learn about operators, don't worry :)) basicly selects _scope_. So, doing std:: just means that you are selecting something that exists _in_ the std namespace.

Using namespace std just means that you are using that namespace, namely, using whatever it contains (classes, functions, stuff). It's basicly doing std:: in everything that's in there, but you leave off the std::.

Say this:

int func(int x) {
std::vector<int> list;

list.push_back(x);
}


This takes a vector from the std namespace and gives it to you. Without the std:: (or using namespace std), it'll tell you that vector does not exist because it cannot find it.

When you make a function that uses namespace std you're just using some really nice things from that namespace - if they were declared outside the namespace then you just wouldn't need the std:: or using part. They are not put in the std namespace.

To delcare a namespace, you do this:

name NAMESPACE_NAME
{
...
SomeFunction();
blablabla...
...
};

SomeOtherFunction() {
return NAMESPACE_NAME::SomeFunction();
}


Note that since SomeFunction is in NAMESPACE_NAME, you must do that, std is simply a namespace name just like that.

You can also use this:
using std::cin;
using std::cout;
using ...;
To use specific ones without the std:: part, instead of using namespace std; entirely.

Good luck, and ask away!

By the way, if it's ok to ask, what book ya reading?

Note: I pressed reply before there were any posts!

C++

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Anonymous Poster
...They are not put in the std namespace.

I mean, the functions you declare :).

And umm... I put name instead of namespace in the code, sorry :).

c++

Share this post


Link to post
Share on other sites
Thanks!

First I read "C++ programming" by Stephen Prata but I didn't quite like it and after 300 pages I stopped. It was ok but I though there must be a better one. And so I found "Thinking in C++" by Bruce Eckel and I'm quite happy with it.

[Edited by - tufflax on January 10, 2006 9:00:50 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by tufflax
If I'm using namespace std, is that like typing std:: in front of everything? Not quite?


Basically. Don't do it in headers though, because you end up with:

1) "Global namespace pollution" - basically, intellisense allways shows everything in the std:: namespace, wheither or not you want it. I believe this label is also applied to the problem I describe bellow (#2):

2) The potential for conflict. When they typed "system", did they mean "std::system", "::system", or "foo::bar::system"? Letting the source-file-writer decide lets them specify what THEY want - and then just type "system" instead of "foo::bar::system" without having any ambiguity conflicts because some header did "using namespace std;" thus making std::system a potential as well.

Quote:
What does ::something mean?


"something in the root namespace".

if you have:

namespace foo {
int bar; //int #1
}
namespace bar {
namespace foo {
int bar; //#2
}
int bar; //#3
}

//boring example:
//can refer to #1 as: foo::bar, ::foo::bar
//can refer to #2 as: bar::foo::bar, ::bar::foo::bar
//can refer to #3 as: bar::bar, ::bar::bar

namespace foo {
//semi-interesting example (shows resolution of conflicts):
//can refer to #1 as: bar, foo::bar, ::foo::bar
//can refer to #2 as: ::bar::foo::bar
// (note: bar::foo::... does not work as "bar" is an int locally (first token))
//can refer to #3 as: ::bar::bar
// (note: bar::bar does not work as "bar" is an int locally (first token))
}

namespace bar {
//really interesting example (shows meaning change while both are valid)
//can refer to #1 as: ::foo::bar
//can refer to #2 as: foo::bar, ::bar::foo::bar
//can refer to #3 as: bar, ::bar::bar

namespace foo {
//meh
//can refer to #1 as: ::foo::bar
//can refer to #2 as: bar, foo::bar, ::bar::foo::bar
//can refer to #3 as: bar::bar
}
}


Quote:
If I make a function in a source file that is using namespace std, will that function be in namespace std or some place else? Why?


It will be in the default/root namespace. Using only "imports" that namespace to whatever namespace you're in locally. It does not change your current namespace to the one selected. This allows you to use multiple namespaces:

#include <boost/functon.hpp>
#include <iostream>

using namespace std;
using namespace boost;

void foo ( void ) {
cout << "hi" << endl; //cout/endl are in namespace std
function< void( void ) > a = & foo; //function< T > is in namespace boost
function< void( void ) > b = & std::foo; //error
function< void( void ) > c = & boost::foo; //error
function< void( void ) > c = & ::foo; //ok

}


You can enter a namespace (which can access itself and parents (but not siblings) without the std::/boost::/mynamespace:: prefixes) by opening the block again:

namespace foo {
int bar;
}

namespace foo {
void baz( void ) {
++bar;
}
void baz2( void ) {
baz(); //ok
foo::baz(); //ok
::baz(); //error
::foo::baz(); //ok
}
}

Share this post


Link to post
Share on other sites
Wow, if every answer would be that good :D

But one thing...

Quote:
namespace bar {
//really interesting example (shows meaning change while both are valid)
//can refer to #1 as: ::foo::bar
//can refer to #2 as: foo::bar, ::bar::foo::bar
//can refer to #3 as: bar, ::bar::bar

namespace foo {
//meh
//can refer to #1 as: ::foo::bar
//can refer to #2 as: bar, foo::bar, ::bar::foo::bar
//can refer to #3 as: bar::bar
}
}


Is the last "//can refer to #3 as: bar::bar" really correct? Isn't it bar the int that is the first token?

Really good answer. Thanks a lot!

Share this post


Link to post
Share on other sites

This topic is 4360 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this