Sign in to follow this  

To get in the habit which coding structure should I use (C++) [source]

This topic is 3631 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

Im reading Beginning C++ Game Programming by Michael Dawson and he gives three examples of using namespaces. From a speed standpoint does it really matter if I use #1 (introducing "std::" before using the element), #2 (introducing "using namespace") or #3(introducing "using namespace::element"? 1: #include <iostream> int main() { std::cout << "Game Over!" << std::endl; return 0; } ---------------------------------------------- 2: #include <iostream> using namespace std; int main() { cout << "Game Over!" << endl; return 0; } ---------------------------------------------- 3: #include <iostream> using std::cout; using std::endl; int main() { cout << "Game Over!" << endl; return 0; }

Share this post


Link to post
Share on other sites
Don't start worrying about speed when you're writing a simple "hello world" console application!

To answer your question, they are, as SiCrane said equivalent but, until you know the side-effects of using "using namespace" statements (especially in header files), you should use method 1, or method 3 (which isn't seen so often but is still valid).

EDIT : If you're really worried about speed, "\n" should be faster than std::endl (Ok I'll shut up now)

Share this post


Link to post
Share on other sites
Your thread title does not exactly correspond with your actual question.
It's not an issue of compile or runtime speed, it's about namespace collisions.

A good habit is to use std::element, and on some occasion using std::element, the latter only within a file/function scope and never globally in a header file.

If you want to know the reasons for that and maybe some background, visit this FAQ, it's a throughout nice coding practice reference for beginners.

Share this post


Link to post
Share on other sites
As SiCrane already said, the three compile to the same code.

As far as compiler speed goes, the first will be negligibly faster than the third, which will be slightly faster than the second.

Share this post


Link to post
Share on other sites

This topic is 3631 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