首页 > 技术文章 > C++命名空间

moonlightpoet 2016-07-08 19:39 原文

样例:

#include <iostream>
using namespace std;
namespace Jack {
    int age;
    void sayHello() {
        cout << "hello, I'm Jack" << endl;
    }
}
namespace Johns {
    int age = 20;
    void sayHello() {
        cout << "hello, I'm Johns" << endl;
    }
}
int main() {
    Jack::age = 10;
    cout << Jack::age << endl;
    Jack::sayHello();
    cout << Johns::age << endl;
    using Johns::sayHello;
    sayHello();
    return 0;
}

 

推荐阅读