Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learn C++ (The Correct Way) - Lesson 1
#11
(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..
Reply
#12
Because it's just huge waste of space on my screen Omg.

Great tutorial I'll go through it when I get home ;).
Reply
#13
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
Reply
#14
Thanks guys, ill transfer part 3 from HF later on.
Quote:Who has two thumbs and doesn't give a crap.......Bob Kelso......Have we met?
[Image: BobKelso.jpg]
Reply
#15
Love the tutorial. Easy to understand
lazydude2000 Wrote:I hate Minnesota...

Omniscient Wrote:btw...I grew up in Minnesota.
Reply
#16
(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.
[Image: sig.php]
The little boat gently drifted across the pond exactly the way a bowling ball wouldn't.
Reply
#17
Thanks much man
Appreciate it
Reply
#18
nice tut. should help alot of people
Reply
#19
Nice bro, this should help alot of members out
Reply
#20
(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...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Learn C++ (The Correct Way) - Lesson 2 Code King 12 3,837 04-07-2012, 06:05 PM
Last Post: AceInfinity
  At What Age Did You Learn C++ Win 66 20,696 12-11-2011, 02:52 PM
Last Post: Neel

Forum Jump:


Users browsing this thread: 5 Guest(s)