#include <iostream>
#include <string>
#include <algorithm>
// c doesn't have namespace.
// c++: to avoid naming conflicts.
// the print() functions are identical symbols, without using different namespace it'll cause a linking error.
namespace apple {
void print(const char* text){ std::cout << text << std::endl; }
void secondFunc(){}
}
namespace orange {
void print(const char* text)
{
std::string temp = text;
std::reverse(temp.begin(), temp.end());
std::cout << temp << std::endl;
}
}
int main(void)
{
// an useful way:namespace shortName = nested longName.
namespace a = apple;
// 3 ways of using namespace.
//using namespace apple; //1
//using apple::print; //2
apple::print("I am Groot"); //3
a::print("a");
std::cin.get();
}
【Cherno的C++视频】Namespace in C++
最新推荐文章于 2024-10-18 22:48:33 发布