How to use #ifndef #define, etc, in a macro definition

P

Peng Yu

I want to define a macro

#define FILEWR(FILENAME)

which can be expanded to

#ifndef OUTFILE
#define OUTFILE
ofstream out;
#endif
out.open(FILENAME);
out << "A" << endl;
out.close();


Do you have any idea how to do this? Thanks!

Peng
 
J

John Harrison

Peng Yu said:
I want to define a macro

#define FILEWR(FILENAME)

which can be expanded to

#ifndef OUTFILE
#define OUTFILE
ofstream out;
#endif
out.open(FILENAME);
out << "A" << endl;
out.close();


Do you have any idea how to do this? Thanks!

Impossible, you cannot use pre-processing directives in a macro.

john
 
J

JKop

Peng Yu posted:
I want to define a macro

#define FILEWR(FILENAME)


inline void FileWrite(const char* const filename)
{
#ifndef OUTFILE

std::eek:fstream 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::eek:fstream 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
 
M

Method Man

Alternatively, you could make it static:
inline void FileWrite(const char* const filename)
{
#ifndef OUTFILE

static std::eek:fstream out;

#define OUTFILE
#endif

out.open(filename);
out << "A\n";
out.close();
}

In this case, the #ifndef ... #endif is not needed since static objects are
declared only once within their scope anyway.
 
J

JKop

Method Man posted:
In this case, the #ifndef ... #endif is not needed since static objects
are declared only once within their scope anyway.

....read my last post. Pay particular attention to the part that mentions the
global variable.


-JKop
 
O

Old Wolf

Peng Yu said:
I want to define a macro

#define FILEWR(FILENAME)

which can be expanded to

#ifndef OUTFILE
#define OUTFILE
ofstream out;
#endif
out.open(FILENAME);
out << "A" << endl;
out.close();

Have you considered:

#define FILEWR(FILENAME) \
{ ofstream out; out.open(FILENAME); out << "A\n"; }

(the stream is automatically closed and flushed when it goes out of
scope), or better (?) is

#define FILEWR(FILENAME) (ofstream(FILENAME) << "A\n")

(BTW this is stupid because of no error-checking or exception
handling; you should use a function)
 
M

Method Man

JKop said:
Method Man posted:


...read my last post. Pay particular attention to the part that mentions the
global variable.

Sorry I misread your post; I didn't know you were referring back to the
global object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top