口诀: extern表示声明,可以多次
//main.cpp
#include <iostream>
#include "A.h"
namespace mmspam {
extern A a;
void f() {
std::cout<<a.x<<std::endl;
}
}
void f2() {
std::cout<<mmspam::a.x<<std::endl;
}
int main() {
mmspam::f();
f2();
//std::cout<<y<<std::endl;
return 0;
}
//
// Created by yilian(练意) on 2018/12/21.
//
//A.cpp
#include "A.h"
//using namespace mmspam;
namespace mmspam {
A a(20);
}
mmspam::A a(30);
//
// Created by yilian(练意) on 2018/12/21.
//
//A.h
#ifndef UNTITLED19_A_H
#define UNTITLED19_A_H
namespace mmspam {
class A {
public:
A(int x):x(x){}
int x;
};
extern A a;
}
#endif //UNTITLED19_A_H