Peng Yu posted:
I want to define a macro
#define FILEWR(FILENAME)
inline void FileWrite(const char* const filename)
{
#ifndef OUTFILE
std::ofstream out;
#define OUTFILE
#endif
out.open(filename);
out << "A\n";
out.close();
}
Here's how it would work:
If you're going to define a global object, you must do it as so:
ofstream out;
#define OUTFILE
If the global object is already defined, it will be used by the function.
If the global object is *not* already defined, an object called "out", which
will be a local object of the function and which will of automatic duration
(ie. it will be destroyed at the end of the function), will be defined and
used by the function.
Alternatively, you could make it static:
inline void FileWrite(const char* const filename)
{
#ifndef OUTFILE
static std::ofstream out;
#define OUTFILE
#endif
out.open(filename);
out << "A\n";
out.close();
}
Note that I don't know what you're doing... and I don't want to know!
-JKop