#define只做替换,好好体会
#include <iostream>
#include <map>
using namespace std;
class A{
public:
A() {
//cout << "A" << endl;
}
A(const A& a) {
cout << "A(const A& a)" << endl;
}
A& operator=(const A&) {
cout << "operator" << endl;
return *this;
}
int a_;
int pre_a_;
};
#define FF(a, b) do {\
m[a"b"] = b;\
} while(0)
//不加字符串会解释成变量,没有定义变量ac,编译报错
#define FF1(a, b) do {\
m[ac] = b;\
} while(0)
#define FF2(a, b) do {\
m[#a] = b;\
} while(0)
#define HH(g, b) do {\
cout << g.a_ << endl;\
} while(0)
//找不到成员变量,编译错误
#define HH1(g, b) do {\
cout << g.ba_ << endl;\
} while(0)
#define HH2(g, b) do {\
cout << g.b##a_ << endl;\
} while(0)
//找不到成员变量,编译错误
#define HH3(g, b) do {\
cout << g.b"a_" << endl;\
} while(0)
int main() {
map<string, int> m;
FF(, 1);
FF("v1_", 2);
FF2(cc, 3);
for (map<string, int>::iterator it = m.begin(); it != m.end(); ++it) {
cout << it->first << " " << it->second << endl;
}
A aa;
HH(aa, b);
//HH3(aa, pre_);
return 0;
}
b 1
cc 3
v1_b 2
A
1