By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,687 Members | 1,129 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,687 IT Pros & Developers. It's quick & easy.

How to call a virtual base function with variable parameters?

P: n/a
Hi,

How can I call a virtual base function with variable parameters? like,

class base
{
public:
int printf(const char *fmt, ...)
{
// do anything here.
}
};

class derived : public base
{
public:
int printf(const char *fmt, ...)
{
// do something here.
...
// then call base function
return base::printf(fmt, XXX); // WHAT CAN I WRITE HERE?
}
};

Thanks in advance,
rbfish

Jan 3 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
rbf...@hotmail.com wrote:
Hi,

How can I call a virtual base function with variable parameters? like,

class base
{
public:
int printf(const char *fmt, ...)
{
// do anything here.
}
};

class derived : public base
{
public:
int printf(const char *fmt, ...)
{
// do something here.
...
// then call base function
return base::printf(fmt, XXX); // WHAT CAN I WRITE HERE?
}
};

Thanks in advance,
rbfish


You'll need to use the <cstdarg> facilities. See your favorite C text
book for more, since C++ discourages using variable args. (A C++
approach would be to use iostreams, which, unlike printf and its
ellipses, are type-safe.)

Cheers! --M

Jan 3 '06 #2

P: n/a

rb****@hotmail.com wrote:
Hi,

How can I call a virtual base function with variable parameters? like,

class base
{
public:
int printf(const char *fmt, ...)
{
// do anything here.
}
};

class derived : public base
{
public:
int printf(const char *fmt, ...)
{
// do something here.
...
// then call base function
return base::printf(fmt, XXX); // WHAT CAN I WRITE HERE?
}
};


You can't. It's as simple as that. For ellipsis arguments, the compiler
still
needs to know the exact arguments at the call site, even if the
function
called doesn't know them at compile time.

In this case, derived::printf doesn't have the arguments available at
compile
time to call base::printf. Have a look at more modern solutions like
iostream
or boost::format.

HTH,
Michiel Salters

Jan 4 '06 #3

P: n/a
I just used printf() as an example. Actually I liked to use it in the
other way.
Anyway, thank you very much.

Jan 4 '06 #4

P: n/a
rb****@hotmail.com wrote:
I just used printf() as an example. Actually I liked to use it in the
other way.


What other way? The << and >> operators can still be used to pass
parameter lists of variable length in a more "C++-ish" way than vararg.

--
Mike Smith
Jan 5 '06 #5

P: n/a

rb****@hotmail.com wrote:
I just used printf() as an example. Actually I liked to use it in the
other way.
Anyway, thank you very much.


Well your function above isn't even described as virtual but you could
have a non-virtual method in the base-class that uses ... which then
expands to var_args and calls a virtual method. Something like;

class base
{
public:
void print( const std::string & format, ... )
{
va_list args;
va_begin( args, format ); // or whatever it is
do_print( format, args );
}
protected:
virtual void do_print( const std::string & format, va_list args );
};

A better way to print a variable argument list though is to get the
"variable" parameter to be some kind of collection. You can use an
operator like + between the arguments.

Jan 5 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.