把变量Value放到1.cpp的命名空间下,在2.cpp中main函数中访问变量Value,不允许直接访问和赋值(调用函数访问)
1.h
#ifndef _ONE_H
#define _ONE_H
namespace myspace
{
extern int value;
int getValue();
void setValue(int x);
}
#endif
1.cpp
namespace myspace
{
int Value = 1;
int getValue()
{
return Value;
}
void setValue(int x)
{
Value = x;
}
}
2.cpp
#include <iostream>
#include "1.h"
using namespace std;
using namespace myspace;
int main(int argc, char *argv[])
{
setValue(10);
cout<<getValue()<<endl;
return 0;
}