Hi,
I have a class (which I have no control of) and I would like to create a descendant of this class. The base class has functions with variable parameter lists which I would like to override. I need to then call the original function from within the overridden function.
Is this even possible?
Here is an example I wrote
I have a class (which I have no control of) and I would like to create a descendant of this class. The base class has functions with variable parameter lists which I would like to override. I need to then call the original function from within the overridden function.
Is this even possible?
Here is an example I wrote
Code:
#include <stdarg.h>
//The base class which I cannot chage
class A
{
protected:
int m_iId;
public:
A(int iId)
{
m_iId = iId;
}
virtual ~A() {}
virtual void printId(const char* szFormat, ...) const
{
va_list args;
va_start(args, szFormat);
char szBuffer[2048];
_vsnprintf(szBuffer, sizeof(szBuffer), szFormat, args);
printf("%d\n", m_iId);
printf("%s\n", szBuffer);
va_end(args);
}
};
//This is my derived class and I have full control over this one.
class B : public A
{
public:
B() : A(1) {}
virtual ~B() {}
virtual void printId(const char* szFormat, ...) const
{
printf("---------CLASS B HEADER------------");
//How do I call A::printId(const char* szFormat, ...) with the arguments
//passed to B::printId(const char* szFormat, ...)?
//this obviously doesn't work
A::printId(szFormat, ...);
printf("---------CLASS B FOOTER------------");
}
};
int main(int argc, char* argv[])
{
A a(5);
B b;
a.printId("That was the Id for class <%s>", "A");
b.printId("That was the Id for class <%s>", "B");
return 0;
}