I want to define a macro
#define FILEWR(FILENAME )
which can be expanded to
#ifndef OUTFILE
#define OUTFILE
ofstream out;
#endif
out.open(FILENA ME);
out << "A" << endl;
out.close();
Do you have any idea how to do this? Thanks!
Peng 6 3170
"Peng Yu" <pe*******@gmai l.com> wrote in message
news:nf******** *************** *********@4ax.c om... I want to define a macro
#define FILEWR(FILENAME )
which can be expanded to
#ifndef OUTFILE #define OUTFILE ofstream out; #endif out.open(FILENA ME); 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
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(filena me);
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(filena me);
out << "A\n";
out.close();
}
Note that I don't know what you're doing... and I don't want to know!
-JKop
> Alternatively, you could make it static: inline void FileWrite(const char* const filename) { #ifndef OUTFILE
static std::ofstream out;
#define OUTFILE #endif
out.open(filena me); 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.
Method Man posted: Alternatively, you could make it static:
inline void FileWrite(const char* const filename) { #ifndef OUTFILE
static std::ofstream out;
#define OUTFILE #endif
out.open(filena me); 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.
....read my last post. Pay particular attention to the part that mentions the
global variable.
-JKop
Peng Yu <pe*******@gmai l.com> wrote: I want to define a macro
#define FILEWR(FILENAME )
which can be expanded to
#ifndef OUTFILE #define OUTFILE ofstream out; #endif out.open(FILENA ME); out << "A" << endl; out.close();
Have you considered:
#define FILEWR(FILENAME ) \
{ ofstream out; out.open(FILENA ME); out << "A\n"; }
(the stream is automatically closed and flushed when it goes out of
scope), or better (?) is
#define FILEWR(FILENAME ) (ofstream(FILEN AME) << "A\n")
(BTW this is stupid because of no error-checking or exception
handling; you should use a function)
"JKop" <NU**@NULL.NULL > wrote in message
news:QJ******** ***********@new s.indigo.ie... Method Man posted:
Alternatively, you could make it static:
inline void FileWrite(const char* const filename) { #ifndef OUTFILE
static std::ofstream out;
#define OUTFILE #endif
out.open(filena me); 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.
...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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Evan |
last post by:
Is there any standard for when to use #if
!defined(SOME_INCLUSION_GUARD) versus #ifndef SOME_INCLUSION_GUARD?
Aside from being able to combine multiple things into an #if, is there
any difference? My VC++ docs say that thay are equivalent, but is
there any convention on when to use one vs. another?
Also, how widely is #pragma once supported?
|
by: prettysmurfed |
last post by:
Hi all
I have this, probably stupid question, how to avoid multiple
definitions when a header file is included more than once.
I thought, when you wrote the header-file and used the
ifndef/define directives, the code between the statements
define and endif was only compiled once. But this seems
to be incorrect, as I get a whole bunch of errors when
I compile the critter.
For example if I include header.h more than once the
|
by: DrUg13 |
last post by:
#ifndef HEADER_H
#define HEADER_H
blah blal
#endif
So this tells the compiler That if its defined do not define it again.
Could someone help me understand this.
Does this mean that if I use the same clasee in two different classes
|
by: baumann |
last post by:
hi all,
typedef int (*pfunc)(int , int);
pfunc a_func;
i know it's ok,
but how can define a_func without typedef statement?
thanks .
|
by: marcwentink |
last post by:
Dear people,
The code below is compiling:
#define BUF_SZ 16383
.....
strcat(ConnectString, "Content-Length: BUF_SZ\n");
but it does not work since it give me:
| |
by: Frank Silvermann |
last post by:
#define q p
I must admit to a most elementary confusion. I have only ever used
#define to, in point of fact, #redefine . The above is standard. Is
the following:
#define UNNAMED_OS_32_LEAN_AND_MEAN
? frank
--------
|
by: Sensei |
last post by:
Hi! I'm concerned about the legality of such a definition:
#define funcX funcY
where funcX belongs to the *standard* C functions. Is it legal to do
this? The standard says "any function declared in a header may be
additionally implemented as a macro defined in the header, so a library
function should not be declared explicitly if its header is included".
Is this applicable to standard functions? And, what if the definition
|
by: Mohammad Omer Nasir |
last post by:
I was read Linux kernel code in which I saw few define
macro defines in functions. The snap of the function is following and
file name "err_ev6.c".
static int
ev6_parse_cbox(u64 c_addr, u64 c1_syn, u64 c2_syn,
u64 c_stat, u64 c_sts, int print)
{
char *sourcename = { "UNKNOWN", "UNKNOWN", "UNKNOWN",
"MEMORY", "BCACHE", "DCACHE",
|
by: venkat |
last post by:
I have come across some preprossor statements, such as
#define PPTR_int
#define PPTR_str
#define DDAR_baddr &
#define DDAR_caddr &
What do they mean, but when i compile the code with these i
am not getting any errors .
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |