C++/Language/NameSpace
Содержание
- 1 A namespace can be nested within another
- 2 Defines and tests namespaces.
- 3 Demonstrate a namespace
- 4 Enclosure variables with namespace
- 5 Namespace code section
- 6 Namespace Demo: define a namespace
- 7 Namespaces are additive
- 8 Some Namespace Options
- 9 use explicit std:: qualification.
- 10 Use namespaces to wrap variables
- 11 Using namespace to reference variables
A namespace can be nested within another
<source lang="cpp">
- include <iostream>
using namespace std; namespace MyNameSpace1 {
int i; namespace MyNameSpace2 { // a nested namespace int j; }
} int main() {
MyNameSpace1::i = 19; MyNameSpace1::MyNameSpace2::j = 10; cout << MyNameSpace1::i << " "<< MyNameSpace1::MyNameSpace2::j << "\n"; using namespace MyNameSpace1; cout << i * MyNameSpace2::j; return 0;
}
</source>
Defines and tests namespaces.
<source lang="cpp">
- include <string>
namespace MySpace {
std::string mess = "Within MySpace"; int count = 0; double f( double);
} namespace YourSpace {
std::string mess = "Within YourSpace"; void f( ) { mess += "!"; }
} namespace MySpace {
int g(void); double f( double y) { return y / 10.0; }
} int MySpace::g( ) {
return ++count;
}
- include <iostream>
int main() {
std::cout<< MySpace::mess << std::endl; MySpace::g(); std::cout << "\nReturn value g(): " << MySpace::g() << "\nReturn value f(): " << MySpace::f(1.2) << std::endl; YourSpace::f(); std::cout << YourSpace::mess << std::endl; return 0;
}
</source>
Demonstrate a namespace
<source lang="cpp">
- include <iostream>
using namespace std; namespace MyNameSpace {
int upperbound; int lowerbound; class counter { int count; public: counter(int n) { if(n <= upperbound) count = n; else count = upperbound; } void reset(int n) { if(n <= upperbound) count = n; } int run() { if(count > lowerbound) return count--; else return lowerbound; } };
} int main() {
MyNameSpace::upperbound = 100; MyNameSpace::lowerbound = 0; MyNameSpace::counter ob1(10); int i; do { i = ob1.run(); cout << i << " "; } while(i > MyNameSpace::lowerbound); cout << endl; MyNameSpace::counter ob2(20); do { i = ob2.run(); cout << i << " "; } while(i > MyNameSpace::lowerbound); cout << endl; ob2.reset(100); MyNameSpace::lowerbound = 90; do { i = ob2.run(); cout << i << " "; } while(i > MyNameSpace::lowerbound); return 0;
}
</source>
Enclosure variables with namespace
<source lang="cpp">
- include <iostream>
using namespace std; namespace first {
int x = 5; int y = 10;
} namespace second {
double x = 3.1416; double y = 2.7183;
} int main () {
using first::x; using second::y; cout << x << endl; cout << y << endl; cout << first::y << endl; cout << second::x << endl; return 0;
}
</source>
Namespace code section
<source lang="cpp">
- include <iostream>
using namespace std; namespace first {
int x = 5;
} namespace second {
double x = 3.1416;
} int main () {
{ using namespace first; cout << x << endl; } { using namespace second; cout << x << endl; } return 0;
}
</source>
Namespace Demo: define a namespace
<source lang="cpp">
- include <iostream>
using namespace std;
namespace MyNameSpace {
class demo { int i; public: demo(int x) { i = x; } void seti(int x) { i = x; } int geti() { return i; } }; char str[] = "Illustrating namespaces\n"; int counter;
} namespace SecondNamespace {
int x, y;
} int main() {
MyNameSpace::demo ob(10); // use scope resolution cout << "Value of ob is : " << ob.geti(); cout << endl; ob.seti(99); cout << "Value of ob is now : " << ob.geti(); cout << endl;
using MyNameSpace::str; // bring str into current scope cout << str; using namespace MyNameSpace; // bring all of MyNameSpace into current scope for(counter = 10; counter; counter--) cout << counter << " "; cout << endl; SecondNamespace::x = 10; // use SecondNamespace namespace SecondNamespace::y = 20; cout << "x, y: " << SecondNamespace:: x; cout << ", " << SecondNamespace::y << endl; using namespace SecondNamespace; // bring another namespace into view demo xob(x), yob(y); cout << "xob, yob: " << xob.geti() << ", "; cout << yob.geti() << endl; return 0;
}
</source>
Namespaces are additive
<source lang="cpp">
- include <iostream>
using namespace std; namespace Demo { // In Demo namespace
int a;
} int x; // this is in global namespace namespace Demo {
int b; // this is in of Demo namespace, too
} int main() {
using namespace Demo; a = b = x = 100; cout << a << " " << b << " " << x; return 0;
}
</source>
Some Namespace Options
<source lang="cpp">
- include <iostream>
using namespace std; namespace MyNameSpace {
int i;
} namespace MyNameSpace {
int j;
} int main() {
MyNameSpace::i = MyNameSpace::j = 10; cout << MyNameSpace::i * MyNameSpace::j << "\n"; using namespace MyNameSpace; cout << i * j; return 0;
}
</source>
use explicit std:: qualification.
<source lang="cpp">
- include <iostream>
int main() {
double val; std::cout << "Enter a number: "; std::cin >> val; std::cout << "This is your number: "; std::cout << val; return 0;
}
</source>
Use namespaces to wrap variables
<source lang="cpp">
- include <iostream>
using namespace std; namespace first {
int var = 5;
} namespace second {
double var = 3.1416;
} int main () {
cout << first::var << endl; cout << second::var << endl; return 0;
}
</source>
Using namespace to reference variables
<source lang="cpp">
- include <iostream>
using namespace std; namespace first {
int x = 5; int y = 10;
} namespace second {
double x = 3.1416; double y = 2.7183;
} int main () {
using namespace first; cout << x << endl; cout << y << endl; cout << second::y << endl; cout << second::x << endl; return 0;
}
</source>