Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to call a virtual base function with variable parameters?

Reply
Thread Tools

How to call a virtual base function with variable parameters?

 
 
rbfish@hotmail.com
Guest
Posts: n/a
 
      01-03-2006
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:rintf(fmt, XXX); // WHAT CAN I WRITE HERE?
}
};

Thanks in advance,
rbfish

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      01-03-2006
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:rintf(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

 
Reply With Quote
 
 
 
 
Michiel.Salters@tomtom.com
Guest
Posts: n/a
 
      01-04-2006

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:rintf(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:rintf doesn't have the arguments available at
compile
time to call base:rintf. Have a look at more modern solutions like
iostream
or boost::format.

HTH,
Michiel Salters

 
Reply With Quote
 
rbfish@hotmail.com
Guest
Posts: n/a
 
      01-04-2006
I just used printf() as an example. Actually I liked to use it in the
other way.
Anyway, thank you very much.

 
Reply With Quote
 
Mike Smith
Guest
Posts: n/a
 
      01-05-2006
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
 
Reply With Quote
 
Earl Purple
Guest
Posts: n/a
 
      01-05-2006

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: Rationale for base class pure virtual function call fromctor/dtor being undefined behaviour. gwowen C++ 6 01-18-2012 07:14 PM
virtual overloaded functions and base class function call... alexandre.braganti@gmail.com C++ 20 03-10-2006 04:23 PM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 2 12-01-2003 06:31 AM
Virtual function 'BasicMidReader::~BasicMidReader()' conflicts with base class 'base 'TMemoryStream' tomek C++ 3 11-30-2003 12:18 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57