Support Forums

Full Version: Learn C++ (The Correct Way) - Lesson 1
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5
(10-08-2009, 08:21 AM)MrD. Wrote: [ -> ]Wow, why quote all of that?

Lol this kid, does it matter? Not too much at all now does it, just wanted to have a post? Ahh..
Because it's just huge waste of space on my screen Omg.

Great tutorial I'll go through it when I get home ;).
Great stuff! I hope someone with some c++ skills and skype can be my teacher Big Grin I really wanna learn this stuff, and all the pages i've tried just doesn't teach good enough for me, i need a real person. I've got the basics of alot of stuff already, including c++. someone? plz? Big Grin
Thanks guys, ill transfer part 3 from HF later on.
Love the tutorial. Easy to understand
(10-08-2009, 08:56 AM)Guerriero420 Wrote: [ -> ]Lol this kid, does it matter?
"Kid"... I'm 21, hardly a kid.

Anyway, it matters to me because I don't want to wind up with joint pain from excessive scrolling (which I do get), excuse me for being so selfish.
Thanks much man
Appreciate it
nice tut. should help alot of people
Nice bro, this should help alot of members out
(10-06-2009, 03:29 PM)MrD. Wrote: [ -> ]Actually, that's wrong. Using a "using namespace" IS namespace pollution. This isn't really an issue in a cpp file since it will only be polluted local to that file (unless you are using a unity build); however using it inside a header file is much worse since not only does all the contents of the std library then pollute your header file at a global scope, it also pollutes any files that include that header file. I never use "using namespace" inside a header file, even for my own namespaces (I do use it for my own namespaces inside a cpp file though).

Essentially namespace pollution gets it's name from the fact that it pollutes the "global scope" with other functions, classes, structs, enums, typedefs, etc, that were not originally there. This can lead to easily avoidable namespace conflicts.

For (a pointless) example, say you had your own class called "string" inside your own namespace called "mine", you also include the std:Confusedtring header file. Now this works all well and good, when you need to use your string you specify "mine:Confusedtring", and when you want to use the STL string, you use "std:Confusedtring". You promote your own namespace to the global scope since you know what's in it and everything is hunky dory.

Now, you add a "using namespace std;" to your file. What happens is that everything in the "std" namespace is promoted to global scope so that you can access it without adding the "std" part. You remove the "std::" qualifier and re-compile your code and see the following error.

Quote:error C2872: 'string' : ambiguous symbol could be 'mine:Confusedtring' or 'std:Confusedtring'

Why has this happened? Simple, the compiler now has two classes called "string" in the global scope and it doesn't know which one it should use. It requires you to solve this by explicitly scoping your usage of "string" with the correct namespace.

Code:
#include <iostream>
#include <string>

//! My namespace
namespace mine
{
    //! My string class
    class string {};
}

using namespace std;
using namespace mine;

int main()
{
    string stdString;
    mine::string myString;

    return 0;
}
Won't compile

Code:
#include <iostream>
#include <string>

//! My namespace
namespace mine
{
    //! My string class
    class string {};
}

using namespace std;
using namespace mine;

int main()
{
    std::string stdString;
    mine::string myString;

    return 0;
}
Will compile

I am not saying that using "using namespace" is a bad thing in all cases, it isn't and in most cases it saves a lot of unnecessary typing. I am however saying that you should only really use it for things you know the contents of. I doubt you know the name of everything that exists in the STL, and while the chance of getting a namespace conflict with it is minimal, I still wouldn't recommend it.

Ultimately it boils down to choice, namespaces were added to C++ to avoid conflicts between library code, and your code. Collapsing the namespaces of libraries is for the most part stupid, but it is your choice. It is because it is your choice that I never use a "using namespace" in my header files, because doing so would cause anyone using that header file to have their choice of whether or not they want to pollute their global namespace with my objects taken away from them, and that is not something I want to force on them.

tl;dr "using namespace" is bad in header files as it causes namespace pollution which propagates into other files.

Aside from that, the rest of the lesson is fine. Good job Yeye

if you use different vars(in name) in your own namespace,then it should be fine.If not,you'll meet a big problem in big projects...
Pages: 1 2 3 4 5