writing something like printf() in C++

L

lallous

Hello

I want to write a function that can be called as:

printmessage("hello world, i=" << i << ", j=" << j);

I managed to do it as this:
#define printmessage(fmt) { static stringstream strm; strm << fmt; cout <<
strm.rdbuf() << endl; }

And call it as:

printmessage("hello world, i=" << i << ", j=" << j);


It is not a function though, but it looks like a function and is easily
called.
Could not make it a function as i would have to first create a stringstream
variable then pass its rdbuf() to my printing function.

Can someone suggest a better way?

If I define 'strm' in the macro as 'static' would that be better performance
wise, as the object will not be created / destroyed whenever i am creating a
new scope in that macro?


Why in this:
#define prints(fmt) { s << fmt; cout << s.rdbuf() << endl; }

stringstream s;

prints("hello");

prints("world!");

Why cout << s.rdbuf() shows: "hello\nworld" and not "hello\nhello\nworld\n"
?
 
J

Joe Hotchkiss

lallous said:
Hello

I want to write a function that can be called as:

printmessage("hello world, i=" << i << ", j=" << j);

I managed to do it as this:
#define printmessage(fmt) { static stringstream strm; strm << fmt;
cout << strm.rdbuf() << endl; }

And call it as:

printmessage("hello world, i=" << i << ", j=" << j);


It is not a function though, but it looks like a function and is
easily called.
Could not make it a function as i would have to first create a
stringstream variable then pass its rdbuf() to my printing function.

Can someone suggest a better way?

One possibility would be to use variable argument lists and call it exactly as
you would call printf.

void MsgBox (const char* fmt, ...)
{
char text[1000];
va_list args;

va_start (args, fmt);
vsprintf (text, fmt, args);
va_end (args);

AfxMessageBox (text);
}

--
Regards,

Joe Hotchkiss,
http://joe.hotchkiss.com

XXXXXXXXXXXXXXXXXXXXXXXXX
X joe.hotchkiss X
X at baesystems.com X
XXXXXXXXXXXXXXXXXXXXXXXXX
 
L

lallous

Joe Hotchkiss said:
lallous said:
Hello

I want to write a function that can be called as:

printmessage("hello world, i=" << i << ", j=" << j);

I managed to do it as this:
#define printmessage(fmt) { static stringstream strm; strm << fmt;
cout << strm.rdbuf() << endl; }

And call it as:

printmessage("hello world, i=" << i << ", j=" << j);


It is not a function though, but it looks like a function and is
easily called.
Could not make it a function as i would have to first create a
stringstream variable then pass its rdbuf() to my printing function.

Can someone suggest a better way?

One possibility would be to use variable argument lists and call it exactly as
you would call printf.

void MsgBox (const char* fmt, ...)
{
char text[1000];
va_list args;

va_start (args, fmt);
vsprintf (text, fmt, args);
va_end (args);

AfxMessageBox (text);
}

--
Regards,

Joe Hotchkiss,
http://joe.hotchkiss.com

XXXXXXXXXXXXXXXXXXXXXXXXX
X joe.hotchkiss X
X at baesystems.com X
XXXXXXXXXXXXXXXXXXXXXXXXX

I know about VA but I want now to implement streams as suggested in:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&safe=off&selm=b6
js7e%24qoo%2405%243%40news.t-online.com
 
K

Karl Heinz Buchegger

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

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,436
Latest member
MaxD

Latest Threads

Top