有没有办法让#define 与自定义对象一起工作?
这是一个例子来说明我的意思:
#include <iostream>
#define i Complex z; z.Re=0; z.Im=1 //<-error
using namespace std;
class Complex{
public:
double Re;
double Im;
void operator=(Complex z){
this->Im=z.Im;this->Im=z.Re;
}
};
int main(){
Complex a = i; //<- the "i" (line 15)
cout<< a.Re<< endl<< a.Im;
return 0;
}
在此代码中,它在第 2 行显示错误:“expected primary-expression before 'a'”。
我希望将第 15 行中的“i”替换为具有正确属性的对象。
如果用#define 做类似的工作是不可能的,你能用完全不同的方法来做吗?