How to call a virtual base function with variable parameters?

R

rbfish

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
 
M

mlimber

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
 
M

Michiel.Salters

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
 
R

rbfish

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

Mike Smith

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.
 
E

Earl Purple

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.
 

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
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top