|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
using std namespace; what does it mean?
i've read its from the book but i dont understand
|
|
#2
|
||||
|
||||
|
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;
}
|
|
#3
|
|||
|
|||
|
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;
}
|
|
#4
|
||||
|
||||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > using std namespace; what does it mean? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|