Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,560 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,928 people online right now. Registration is fast and FREE... Join Now!




Syntax?

 
Reply to this topicStart new topic

Syntax?, Namespace...

I3AnThRaX69
11 Apr, 2008 - 06:25 PM
Post #1

New D.I.C Head
*

Joined: 11 Apr, 2008
Posts: 4


My Contributions
I was looking around attempting to learn C++ and I notice within some books people use either...
using namespace std;
and that leads to being able to use the code like...

cout << "HELLO WORLD!\n";

but then I notice sometimes they don't use the using namespace std; but instead use

std::cout << "HELLO WORLD!\n";


What's the difference? Any help would be appreciated.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Syntax?
11 Apr, 2008 - 06:49 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
The :: is a scope resolution operator, it allows the programmer to access items from a Namespace from outside that Namespace. The std Namespace contains the cout method, to access that functionality from outside the std Namespace you have 2 options


cpp

using namespace std;

void main(string[] args)
{
cout<<args[0];
}



Or you can use the scope operator, thus eliminating the need to use the using statement


cpp

void main(string[] args)
{
std::cout<<args[0];
}



Using the scope operator is extremely useful in a situation where 2 (or more) Namespaces contains the same identifier, like say you created 2 Namespaces, and they both had an identifier of Hello, but did different/similar operations. Without the scope operator you could end up with a redefinition error, meaning that one of the Hello uses was trying to redefine the other from the other Namespace.

I hope this makes things a little clearer for you smile.gif

This post has been edited by PsychoCoder: 11 Apr, 2008 - 07:04 PM
User is offlineProfile CardPM
+Quote Post

I3AnThRaX69
RE: Syntax?
11 Apr, 2008 - 07:57 PM
Post #3

New D.I.C Head
*

Joined: 11 Apr, 2008
Posts: 4


My Contributions
Alright makes sense thank you for the quick reply, and help.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/2/08 11:34PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month