| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"tings" |
| Date: |
09 Jan 2005 01:15:07 PM |
| Object: |
How to write such a function? |
How to write such a function that can take varible number and tyoes of
arguments, like printf("... %d, %s...", myInt, myString...)?
Thanks for for your help!
.
|
|
| User: "Siemel Naran" |
|
| Title: Re: How to write such a function? |
09 Jan 2005 03:16:54 PM |
|
|
"tings" <tings668@hotmail.com> wrote in message
news:%ofEd.90206$uM5.33520@bgtnsc05-
How to write such a function that can take varible number and tyoes of
arguments, like printf("... %d, %s...", myInt, myString...)?
In standard C++, the preferred way to do this would be to:
(1) Create an abstract base class Variable with virtual functions, derived
class Int and so on from it, create a std::vector<Variable*> though
std::vector<boost::shared_ptr<Variable> > might be better in terms of memory
management.
(2) Create a std::vector<boost::any>.
If all your types are fundamental types, then you can use the va_start,
va_arg, and va_end macros. Furthermore, if you want to pass a ... list
another function, you can pass the va_list to it. I think it's like this:
void myprintf(const char * format, ...) {
std::cout << "In my printf\n";
va_list ap;
va_start(ap, format);
vprintf(format, va_list);
va_end(ap);
}
.
|
|
|
|
| User: "Alf P. Steinbach" |
|
| Title: Re: How to write such a function? |
09 Jan 2005 02:11:59 PM |
|
|
* tings:
How to write such a function that can take varible number and tyoes of
arguments, like printf("... %d, %s...", myInt, myString...)?
Don't. Use the type-safe idiom exemplified by std::cout. I.e., member
functions or operators that return a reference to the object they're
called on, so that you can tack on further calls.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
.
|
|
|
|
| User: "David Harmon" |
|
| Title: Re: How to write such a function? |
09 Jan 2005 02:09:33 PM |
|
|
On Sun, 09 Jan 2005 19:15:07 GMT in comp.lang.c++, "tings"
<tings668@hotmail.com> wrote,
How to write such a function that can take varible number and tyoes of
arguments, like printf("... %d, %s...", myInt, myString...)?
/* VA_EXAMP.C - variable argument function example, subset of printf() */
/* Released to public domain by author, David Harmon, Oct 1993 */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
void va_example(char *format, ...)
{
va_list ap;
char ch;
va_start(ap, format);
/* Note: because of default promotions, you can't use char,
or float types with va_arg. Use int or double instead. */
while ((ch = *format++) != 0) {
if (ch != '%')
fputc(ch, stdout);
else {
if ((ch = *format++) == 0)
break;
switch (ch) {
case 'd': {
int arg = va_arg(ap, int);
char buf[10];
itoa( arg, buf, 10);
fputs( buf, stdout);
break;
}
case 'c': {
int arg = va_arg(ap, int);
fputc( (char)arg, stdout);
break;
}
case 's': {
char *arg = va_arg(ap, char *);
fputs(arg, stdout);
break;
}
default:
fputc('%', stdout);
fputc(ch, stdout);
}
}
}
va_end(ap);
}
int main(void)
{
va_example("\"%s\" is a string, %c is a char, and %d is an integer.\n",
"Who is John Galt?", '$', -1);
return 0;
}
.
|
|
|
| User: "Siemel Naran" |
|
| Title: Re: How to write such a function? |
09 Jan 2005 03:16:55 PM |
|
|
"David Harmon" <source@netcom.com> wrote in message
void va_example(char *format, ...)
{
va_list ap;
char ch;
va_start(ap, format);
/* Note: because of default promotions, you can't use char,
or float types with va_arg. Use int or double instead. */
while ((ch = *format++) != 0) {
if (ch != '%')
fputc(ch, stdout);
else {
if ((ch = *format++) == 0)
break;
switch (ch) {
case 'd': {
int arg = va_arg(ap, int);
char buf[10];
itoa( arg, buf, 10);
fputs( buf, stdout);
break;
}
Out of curiosity, can one use this method to pass class types? In other
words, is
MyClass arg = va_arg(ap, MyClass);
ok?
.
|
|
|
| User: "Jerry Coffin" |
|
| Title: Re: How to write such a function? |
09 Jan 2005 04:36:27 PM |
|
|
[ ... ]
Out of curiosity, can one use this method to pass class types? In
other
words, is
MyClass arg = va_arg(ap, MyClass);
ok ?
When you're passing a parameter as part of a variable parameter list,
"If the argument has a non-POD class type (clause 9), the behavior is
undefined." ($5.2.2/7).
--
Later,
Jerry.
The universe is a figment of its own imagination.
.
|
|
|
|
|
|
| User: "Artie Gold" |
|
| Title: Re: How to write such a function? |
09 Jan 2005 01:31:51 PM |
|
|
tings wrote:
How to write such a function that can take varible number and tyoes of
arguments, like printf("... %d, %s...", myInt, myString...)?
Thanks for for your help!
See, for example:
http://www.gnu.org/software/libc/manual/html_node/Variadic-Example.html#Variadic%20Example
Of course, you *could* have searched the web for `variadic function
C++'...but I'll give you this one as a freebie. ;-)
[Of course, since this is news:comp.lang.c++, the include you should use
is <cstdarg> as opposed to <stdarg.h>.]
HTH,
--ag
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/20)
http://www.cafepress.com/goldsays
.
|
|
|
|

|
Related Articles |
how do you declare pointer to such nested member function? Any function can convert the words form,such as "boys"->"boy","became->become". checking if the value is an number or a character, is there such function? Any function can convert the words form,such as "boys"->"boy","became->become". write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter Any function can convert the words form,such as "boys"->"boy","became->become"? Is it legal to create such a member template conversion operator? explaination need for if such a qualifier appears at the outermost level of the parameter type, it is simply ignored:
| Is there any such option? Such bug ;/ We are wholesaler top quality of brand sport shoes, clothes,jersey(NBA, NBL) handbags, sunglasses electronic mobile telephone . such as nike,football shoes, Jordan 1-23, Air Jordan, AF1, DUNK, Air max series, puma, adidas,prada,gucci,hogan shoes, iph is such exception handling approach good? Why did the C++ standard committee make such restrictions on bit-fields? valgrind: failed to start tool 'memcheck' for platform 'x86-linux': No such file or directory why catch (...) can not catch such exception
|
|
|