C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 19th, 2002, 08:50 PM
mue2k mue2k is offline
Forever Newbie
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2002
Location: Bangkok, Thailand.
Posts: 4 mue2k User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to mue2k
using std namespace; what does it mean?

i've read its from the book but i dont understand

Reply With Quote
  #2  
Old October 19th, 2002, 09:20 PM
dcaillouet's Avatar
dcaillouet dcaillouet is offline
Big Endian
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2001
Location: Fly-over country
Posts: 1,172 dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 16 h 29 m 5 sec
Reputation Power: 25
Explained as simply as possible, namespaces allows us to group a set of global classes, objects and/or functions under a name. If you specify using namespace std then you don't have to put std:: throughout your code. The program will know to look in the std library to find the object. Namespace std contains all the classes, objects and functions of the standard C++ library.

Without namespace
Code:
#include <iostream>

int main () {
  std::cout << "Hello world!\n";
  return 0;
}


With namespace
Code:
#include <iostream>
using namespace std;

int main () {
  cout << "Hello world!\n";
  return 0;
}

Reply With Quote
  #3  
Old October 19th, 2002, 11:26 PM
Optix Optix is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 36 Optix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
yea and if you wanted, you could also create your own namespaces like so..

Code:
#include <iostream>
using namespace std;

namespace mystuff{
int value = 5;
}

int main()
{
      cout << mystuff::value; //outputs 5
      return 0;
}


or we could have.

Code:
#include <iostream>
using namespace std;
namespace mystuff{
int value = 5;
}
using namespace mystuff;

int main()
{
      cout << value; //outputs 5
      return 0;
}

Reply With Quote
  #4  
Old October 20th, 2002, 10:35 AM
Scorpions4ever's Avatar
Scorpions4ever Scorpions4ever is offline
Banned ;)
Dev Shed God 7th Plane (8000 - 8499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 8,100 Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level)Scorpions4ever User rank is General 16th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 8 h 50 m 41 sec
Reputation Power: 2059
The whole idea of namespaces was to define a new level of scope. Sometimes different libraries and include files (especially from different third party vendors) have the same global variable or function names. For example, a socket library might define a global variable called max_connections to indicate max socket connections allowed. A database library may declare a global variable with the same name to indicate max connections to a database. This causes problems when a programmer wishes to use both libraries in a program. The concept of namespaces was put forward to solve this problem explicitly. The idea goes like this:
Code:
Code from library 1:
namespace  YoyodyneSockLib {
   int  max_connections;
   int  get_connected_state();
   int  sock_func();
   ....
};

Code from library 2:
namespace FoobarDBLib {
   int max_connections;
   int get_connected_state();
   int db_func();
   ....
};

Programmer's code:
#include "yoyodynesocklib"
#include "foobardblib"
using namespace YoyodyneSockLib;
using namespace FoobarDBLib;
...
...
x = db_func();
y = FoobarDBLib::get_connected_state();
...
...
cout << "max sock connections " << YoyodyneSockLib::max_connections << "\n";
cout << "max database connections " << FoobarDBLib::max_connections << "\n";

As you can see from the above code, namespaces were explicitly used to resolve the problem of conflicting function or variable names between the two libraries.
Comments on this post
sizablegrin agrees!

Reply With Quote
  #5  
Old January 23rd, 2009, 09:56 AM
manojG manojG is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2009
Posts: 1 manojG User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 17 sec
Reputation Power: 0
namespacestd

When we specify that “Namespace std contains all the classes, objects and functions of the standard C++ library” Do we mean all the classes, objects and functions of the standard C++ library are contained in Namespace std OR Namespace std refer to these things in standard C++ library? Please tell me
Comments on this post
salem disagrees: Don't dig up old threads if the only thing you can add is "yeah, me too"

Reply With Quote
  #6  
Old January 23rd, 2009, 09:58 AM
bytbox bytbox is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2009
Posts: 129 bytbox User rank is Sergeant Major (2000 - 5000 Reputation Level)bytbox User rank is Sergeant Major (2000 - 5000 Reputation Level)bytbox User rank is Sergeant Major (2000 - 5000 Reputation Level)bytbox User rank is Sergeant Major (2000 - 5000 Reputation Level)bytbox User rank is Sergeant Major (2000 - 5000 Reputation Level)bytbox User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 2 h 36 m 39 sec
Reputation Power: 25
Send a message via Google Talk to bytbox
Quote:
Originally Posted by manojG
When we specify that “Namespace std contains all the classes, objects and functions of the standard C++ library” Do we mean all the classes, objects and functions of the standard C++ library are contained in Namespace std OR Namespace std refer to these things in standard C++ library? Please tell me


This thread is what, 6 years old? You may have set a record. Use google.

Reply With Quote
  #7  
Old June 14th, 2009, 03:11 AM
Joey2250 Joey2250 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 2 Joey2250 Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 12 m 2 sec
Reputation Power: 0
?

When I was in college we complied using g++ and wrote using C++. All we used to do was include the "iostream.h" to be able to use the "cin <<" and "cout >>". We never had "using namespace std" in our programs.

When did that come into play?
Comments on this post
salem disagrees: "When did that come into play?" ANSWER:1998 - where have you been? Or more importantly, what bunch
of stone-age druids were teaching you old-style C++
sizablegrin disagrees: See the Commonly Asked Questions thread and stop handing out bum advice.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > using std namespace; what does it mean?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump



 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT