Jump to content
  • Advertisement
Sign in to follow this  
crashdmj

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

This topic is 3936 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
Advertisement
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
Sorry kiome,
Im still very new to C++. Perhaps I should just read the book front to back then worry about the nitty gritty.
Thanks

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!