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:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #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,173 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: 24
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: 7
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 5th Plane (7000 - 7499 posts)
 
Join Date: Nov 2001
Location: Glendale, Los Angeles County, California, USA
Posts: 7,442 Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level)Scorpions4ever User rank is Major General (70000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 h 5 m 45 sec
Reputation Power: 797
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.

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


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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway