473,320 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to call a virtual base function with variable parameters?

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
5 5778
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Michael Winter | last post by:
Until about 5 minutes ago, I was happy with my knowledge of virtual functions - then I read "Mixing interface and functional inheritance" posted by Kevin L. earlier today. All of a sudden, I found...
5
by: Duck Dodgers | last post by:
Here is my situation class base { }; class child1 { int data; }; class child2 {
9
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
20
by: alexandre.braganti | last post by:
Hello, First sorry for my poor English, I am French ;-) I've got a comprehension problem of what happend in one of the project i'm working on. Basically I've got a class gs_object than has got...
5
by: druberego | last post by:
I read google and tried to find the solution myself. YES I do know that you can get undefined references if you: a) forget to implement the code for a prototype/header file item, or b) you forget...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
3
by: xtrigger303 | last post by:
Hi to all, a newbie question. Is it possible to avoid the virtual mechanism with pointers to members syntax the same way it's done with an explicit call ( obj.Base::Func() )? It would be...
4
by: Javier | last post by:
Hello, is this possible? I have a pure virtual function in the base class (to force the programmers of the derived classes to have this function implemented) but I want to call the derived class...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.