在ANSI C++中,如何将变量名赋给变量名?我想要做的是,如果用户指定了输出文件名,我在那里发送输出,否则发送到屏幕。因此,像:将变量名赋值给变量名
ofstream outFile;
if (outFileRequested)
outFile.open("foo.txt", ios::out);
else
outFile = cout; // Will not compile because outFile does not have an
// assignment operator
outFile << "whatever" << endl;
我试着这样做的微距功能,以及:
#define OUTPUT outFileRequested?outFile:cout
OUTPUT << "whatever" << endl;
但是,这给了我一个编译器错误,以及。
我想我可以为每个输出使用IF-THEN块,但是如果可以的话,我想避免这种情况。有任何想法吗?
这是一个非常优雅的解决方案。谢谢! – user12576 2009-01-09 17:30:38