Namespaces and Organization
Using Declarations
A using declaration brings one selected name from a namespace into the current scope.
using declaration
A using declaration names exactly what should be brought into the current scope.
Using Declarations
using_declarations.cpp
Replay: real traced execution (multi-file project)
#include <iostream>
#include <string>
namespace greetings {
std::string hello(const std::string& name) {
return "Hello, " + name;
}
}
int main() {
using greetings::hello;
std::string name = "Ada";
std::string message = hello(name);
std::cout << "name=" << name << std::endl;
std::cout << "message=" << message << std::endl;
return 0;
}
#include <iostream>
#include <string>
namespace greetings {
std::string hello(const std::string& name) {
return "Hello, " + name;
}
}
int main() {
using greetings::hello;
std::string name = "Bjarne";
std::string message = hello(name);
std::cout << "name=" << name << std::endl;
std::cout << "message=" << message << std::endl;
return 0;
}
#include <iostream>
#include <string>
namespace greetings {
std::string hello(const std::string& name) {
return "Hello, " + name;
}
}
int main() {
using greetings::hello;
std::string name = "Grace";
std::string message = hello(name);
std::cout << "name=" << name << std::endl;
std::cout << "message=" << message << std::endl;
return 0;
}
name ← Ada
10int main() {11 using greetings::hello;1213 std::string name→ Ada = "Ada"; //@name="Bjarne", "Grace"1415 std::string message = hello(nameAda);std::string hello(const std::string& name)
4namespace greetings {5std::string hello(const std::string& nameAda) {6 return "Hello, " + nameAda;7}message ← Hello, Ada
15 std::string message→ Hello, Ada = hello(nameAda);1617 std::cout << "name=" << nameAda << std::endl;18 std::cout << "message=" << messageHello, Ada << std::endl;19 return 0;20}outputname=Ada message=Hello, Ada
name ← Bjarne
10int main() {11 using greetings::hello;1213 std::string name→ Bjarne = "Bjarne";1415 std::string message = hello(nameBjarne);std::string hello(const std::string& name)
4namespace greetings {5std::string hello(const std::string& nameBjarne) {6 return "Hello, " + nameBjarne;7}message ← Hello, Bjarne
15 std::string message→ Hello, Bjarne = hello(nameBjarne);1617 std::cout << "name=" << nameBjarne << std::endl;18 std::cout << "message=" << messageHello, Bjarne << std::endl;19 return 0;20}outputname=Bjarne message=Hello, Bjarne
name ← Grace
10int main() {11 using greetings::hello;1213 std::string name→ Grace = "Grace";1415 std::string message = hello(nameGrace);std::string hello(const std::string& name)
4namespace greetings {5std::string hello(const std::string& nameGrace) {6 return "Hello, " + nameGrace;7}message ← Hello, Grace
15 std::string message→ Hello, Grace = hello(nameGrace);1617 std::cout << "name=" << nameGrace << std::endl;18 std::cout << "message=" << messageHello, Grace << std::endl;19 return 0;20}outputname=Grace message=Hello, Grace