I want to print some strings in functions, but indented depending on how far the depth of the function calls has gone. My solution so far includes a class from this thread like the following:
class DepthCounter
{
static int depth;
public:
DepthCounter(const std::string& name)
{
std::cout << std::string(depth*2, ' ') << name << " // depth " << depth << '\n';
++depth;
}
~DepthCounter()
{
--depth;
}
};
int DepthCounter::depth = 0;
void func1(){
DepthCounter dc("name1");
}
void func2(){
DepthCounter dc("name2");
func1();
}
so the print in the first construction will have 1 depth and the second one 2 depth (indent).
but I want to make this print multiple times. that is
void func2(){
DepthCounter dc("name0");
DepthCounter dc1("name1");
DepthCounter dc2("name2");
DepthCounter dc3("name3");
func1();
}
but I don't find it nice, let alone the fact that this construction increases the depth although it is still in the same function. Is there a better way do have such functionality?
ideally I want something like:
void func1(){
funcX("name5");
}
void func2(){
funcX("name0");
funcX("name1");
funcX("name2");
funcX("name3");
func1();
}
Does anyone know an alternative way?
You could store the function name inside the DepthCounter and provide an operator<< overload to use inside the functions:
class DepthCounter {
static int depth;
std::string m_name;
public:
DepthCounter(const std::string& name) : m_name(name) {
std::cout << std::string(depth*2, ' ') << "->" << m_name << '\n';
++depth;
}
~DepthCounter() {
--depth;
std::cout << std::string(depth*2, ' ') << "<-" << m_name << '\n';
}
template<class T>
friend DepthCounter& operator<<(DepthCounter& dc, const T& val) {
std::cout << std::string(depth*2, ' ') << dc.m_name << ": " << val << '\n';
return dc;
}
};
int DepthCounter::depth = 0;
And use it like so:
void func1(){
DepthCounter dc(__func__);
dc << "here's something";
}
void func2(){
DepthCounter dc(__func__);
dc << "name1";
dc << "name2";
func1();
dc << "some more after calling func1";
}
Demo
Related
I have multiple functions that have long and similar implementations. The only difference is they call different calls, which is basically based on the function name like below.
// A.h
class A : public ParentA
{
public:
explicit A(B& b);
~A() override = default;
// accessors
C does_same_thing1() const;
C does_same_thing2() const;
C does_same_thing3() const;
// ...
}
// A.cpp
C A::does_same_thing1() const
{
...
return xyz.values().thing1();
}
C A::does_same_thing2() const
{
...
return xyz.values().thing2();
}
C A::does_same_thing3() const
{
...
return xyz.values().thing3();
}
I wonder if there's a way to dynamically fill out the functions that are almost the same except the accessors they call (thing1(), thing2(), and thing3(), and this actually happens more than once, not just on the return line) based on their function names. Would this be possible in C++?
Thanks!
You can write one function template and let the caller choose what is to be returned:
template <typename F>
auto foo(F f) {
...
return f(xyz.values());
}
Details depend on details you left out from the question. For example, is the type of xyz.values() available to the caller? Also, it is up to you to let the caller pick f or write wrappers:
auto does_same_thing1() {
foo([](auto& x) { return x.thing1(); }
}
// ... and same for the others
Some options are:
Using an abstract and overriding the parts you require.
Using lambas and passing in the functions your require.
Using template functions is kind of a mix of the two above, but I'll let someone else explain that one.
Create your base class
class Base
{
protected:
int value;
public:
virtual void differentFunction(int mathThing) = 0;
void longFunction()
{
value = 0;
std::cout << "I do a lot of steps" << std::endl;
std::cout << "Step 1" << std::endl;
value++;
std::cout << "Step 2" << std::endl;
value++;
std::cout << "Step 3" << std::endl;
value++;
std::cout << "Step 4" << std::endl;
value++;
std::cout << "Step 5" << std::endl;
value++;
std::cout << "Step 6" << std::endl;
//And finally I do a unique thing
differentFunction(3);
std::cout << "Resulting value: " << value << std::endl;
}
void longLamdaFunction(std::function<void(int& value, int mathThing)> mathFunction)
{
value = 0;
std::cout << "I do a lot of steps" << std::endl;
std::cout << "Step 1" << std::endl;
value++;
std::cout << "Step 2" << std::endl;
value++;
std::cout << "Step 3" << std::endl;
value++;
std::cout << "Step 4" << std::endl;
value++;
std::cout << "Step 5" << std::endl;
value++;
std::cout << "Step 6" << std::endl;
//And finally I do a unique thing
mathFunction(value, 3);
std::cout << "Resulting value: " << value << std::endl;
}
};
Create an overriding class
class Derived1 : public Base
{
public:
void differentFunction(int mathThing) override
{
std::cout << "I multiply the value" << std::endl;
value *= mathThing;
}
};
Create a different overriding class
class Derived2 : public Base
{
public:
void differentFunction(int mathThing) override
{
std::cout << "I divide the value" << std::endl;
value /= mathThing;
}
};
Example on use, you can see the Lambda example here too
int main()
{
Derived1 d1;
Derived2 d2;
std::cout << "\nUsing multiple interface\n";
d1.longFunction();
std::cout << "\nUsing divide interface\n";
d2.longFunction();
std::cout << "\nUsing add lamda\n";
//I now add them
auto addFunction = [](int& x, int y) -> void { x += y; };
d1.longLamdaFunction(addFunction);
std::cout << "\nUsing subtract lamda\n";
//I now subtract them
auto subtractFunction = [](int& x, int y) -> void { x -= y; };
d1.longLamdaFunction(subtractFunction);
}
I have an abstract class Object, which looks like this:
class Object
{
public:
Object();
virtual void DoSomething() = 0;
virtual void Save(std::ofstream &file) = 0;
virtual void Load(std::ifstream &file) = 0;
};
I also have classes Ball and TextBox:
class Ball : public Object
{
public:
Ball(int x, int y, int z)
{
m_x = x; m_y = y; m_z = z;
}
void DoSomething()
{
std::cout << m_x << ", " << m_y << ", " << m_z << std::endl;
}
void Save(std::ofstream &file)
{
file << m_x << " " << m_y << " " << m_z << std::endl;
}
void Load(std::ifstream &file)
{
file >> m_x >> m_y >> m_z;
}
private:
int m_x, m_y, m_z;
};
class TextBox: public Object
{
public:
TextBox(std::string message)
{
m_message = message;
}
void DoSomething()
{
std::cout << m_message << std::endl;
}
void Save(std::ofstream &file)
{
file << m_message << std::endl;
}
void Load(std::ifstream &file)
{
file >> m_message;
}
private:
std::string m_message;
};
I have a vector of pointers to theese objects:
std::vector<Object*> objects;
objects.push_back(new Ball(1, 2, 3));
objects.push_back(new TextBox("Hello world!"));
for (Object *o : objects) o->DoSomething();
I want to save this vector to file. This is very easy. I make something like this:
std::ofstream file("foo.txt");
for (Object *o : objects) o->Save(file);
file.close();
Now I want to load this. And here is a problem. How I can specify, which object (Ball or TextBox) I supposed to load? I could use some indexes, like that:
void Ball::Save(std::ofstream &file)
{
file << 1 << " " << m_x << " " << m_y << " " << m_z << std::endl;
}
void TextBox::Save(std::ofstream &file)
{
file << 2 << " " << m_message << std::endl;
}
Then, after reading this index, I know, that this is a Ball or a TextBox.
int index;
Object *o;
file >> index;
switch (index)
{
case 1: o = new Ball(0, 0, 0); break;
case 2: o = new TextBox(""); break;
}
o->Load(file);
objects.push_back(o);
But is there a better way to store this structure? What if I have 100 inheritors of Object? Can I somehow avoid 100-switch case?
These 'indexes' you came up with are usually called 'metadata'. There are other languages, like java, that support reflection for that purpose but in C++ reflection in still under consideration. For that reason, I would suggest implementing sort of an abstract interface that would return information of the exact class of an object.
Furthermore, I would suggest looking at serialization libraries, that could ease the process of saving and reading data. You can try to start with boost:
https://www.boost.org/doc/libs/1_73_0/libs/serialization/doc/index.html
I must admit that I used boost serialization a long time ago, but I remember it was quite easy to use.
I have a class with a reference and want a getter that returns a pointer.
class X {
std::string& text;
public:
auto GetText() -> decltype(text) * { return &text); // doesn't work
X(std::string& text): text(text) {}
};
The easy way is to pass a pointer to this class. But if I pass a reference, can I get a pointer with a getter?
Edit: here is the error message
error: cannot declare pointer to 'std::__cxx11::string& {aka class std::__cxx11::basic_string<char>&}'
auto GetText() -> decltype(text) * { return &text);
^
Firstly,
auto GetText() -> decltype(text) * { return &text); // doesn't work
is a positively abominable way to declare this signature. Prefer
std::string* GetText(){ return &text);
or even just
auto GetText(){ return &text);
But this isn't code review.
The problem here is that you're asking for a pointer to the declared type of the text member variable which is a string reference (std::string&). From the comment section, it seems you were unaware that decltype respects the 'reference'ness, 'const'ness, and 'volatile'ness of its argument.
You cannot have a pointer to a reference in C++, e.g. std::string&* is malformed. Calling to std::remove_reference_t should resolve that issue, e.g.
auto GetText() -> std::remove_reference_t<decltype(text)> * { return &text);
However, in this case, auto would have correctly deduced your type anyway, so your explicit declaration is unnecessary.
I've made a test program for my initial problem. The program has a class with a pointer and a getter that returns a reference and a second class with a reference and a getter that returns a pointer.
And it seems that -> std::remove_reference_t<decltype(text)> can be substituted with -> decltype(&text).
Feel free to comment.
// g++ main.cpp -o test_reference_pointer && strip -s test_reference_pointer && ./test_reference_pointer
#include <iostream>
// A class with a pointer and a getter that returns a reference.
class A {
std::string *text;
public:
std::string& GetText_old_way() { return *text; }
auto GetText_pure_auto() { return *text; }
auto GetText_pointer_arithmetic() -> decltype(*text) & { return *text; }
public:
A(std::string *text): text(text) {}
};
// A class with a reference and a getter that returns a pointer.
class B {
std::string& text;
public:
std::string *GetText_old_way() { return &text; }
auto GetText_pure_auto() { return &text; }
auto GetText_pointer_arithmetic() -> decltype(&text) { return &text; }
auto GetText_remove_reference() -> std::remove_reference_t<decltype(text)> * { return &text; }
public:
B(std::string& text): text(text) {}
};
int main() {
std::string text = "hello, world";
{//TEST
A a(&text);
unsigned int i{0};
std::cout << "-- Test 1:"<< std::endl;
++i; std::cout << i << ". " << a.GetText_old_way() << std::endl;
++i; std::cout << i << ". " << a.GetText_pointer_arithmetic() << std::endl;
++i; std::cout << i << ". " << a.GetText_pure_auto() << std::endl;
std::cout << std::endl;
}
{//TEST
B b(text);
unsigned int i{0};
std::cout << "-- Test 2:"<< std::endl;
++i; std::cout << i << ". " << *b.GetText_old_way() << std::endl;
++i; std::cout << i << ". " << *b.GetText_pointer_arithmetic() << std::endl;
++i; std::cout << i << ". " << *b.GetText_remove_reference() << std::endl;
++i; std::cout << i << ". " << *b.GetText_pure_auto() << std::endl;
std::cout << std::endl;
}
return 0;
}
I am trying to understand how decorator pattern works and how much I can "stretch" it to me needs. Following this example, I have extended classes XYZ. There exist derived classes "KLM" (from XYZ)
Specifically, even though I have a decorator pattern, the derived decorator classes "KLM" have some functionality that does not show up in any of their base classes "XYZ", "D", "I" or "A".
So while normally I would instantiate an object as
I * inKLM = new L( new M( new K( new A )));
This would not allow me to access the K::doVirtR() , L::doVirtS() and M::doVirtT() functions (see code below). To access these I would need to downcast the inKLM pointer using dynamic_cast to each of classes "KLM".
The problem is that I only manage to do this for the leftmost new in the expression above. I have read that polymorphism needs to be maintained in order for the dynamic casting to work, so I have tried to have a virtual destructor in all functions. Still I cannot get the dynamic cast to work for anything other than the "outer" new operation (in this case object of class "L").
Please see this code. How can I make not only "LinKLM" , but also "MinKLM" and "KinKLM" success in dynamic_casting ?
#include <iostream>
#include <list>
using namespace std;
class D; //decorator base
struct I { //interface (for both Base and DecoratorBase
I(){
cout << "\n| I::ctor ";
}
virtual ~I(){
cout << "I::dtor |" ;
}
virtual void do_it() = 0;
virtual void regDecorator(D* decorator) = 0;
virtual void train() = 0;
virtual void et() = 0;
};
class D: public I { //DecoratorBase : has same-named fns as Base (must be exported on I) and calls upon them.
public:
D(I * inner) : m_wrappee(inner) {
cout << "D::ctor ";
regDecorator(this);
}
virtual ~D() {
cout << "D::dtor ";
delete m_wrappee;
}
void do_it() {
m_wrappee->do_it();
}
virtual void et() {
cout << "filling in for lack of et() in derived class\n";
} //almost pure virtual, just not implemented in all derived classes
void train(){
m_wrappee->train();
}
private:
void regDecorator(D* decorator){
m_wrappee->regDecorator(decorator);
}
I * m_wrappee;
};
class A: public I { //Base has all the basic functionality
public:
A() {
cout << "A::ctor " ;
decList.clear();
}
~A() {
cout << "A::dtor |" ;
}
void do_it() {
cout << 'A';
}
void train(){
et();
}
void regDecorator(D* decorator)
{
if (decorator) {
cout << "reg=" << decorator << " ";
decList.push_back(decorator);
}
else
cout << "dec is null!" <<endl;
}
private:
void et()
{
//size_t counter=0;
list<D*>::iterator it;
for( it=decList.begin(); it != decList.end(); it++ )
{
//if ( (*it)->et() )
(*it)->et();
//else
// cout << "couldnt et cnt=" << counter << endl;
//counter++;
}
}
std::list<D*> decList;
};
class X: public D { //DerivedDecoratorX ..
public:
X(I *core): D(core){
cout << "X::ctor ";
}
virtual ~X() {
cout << "X::dtor ";
}
void do_it() {
D::do_it();
cout << 'X';
}
void doX() {
cout << "doX" << endl;
}
protected:
virtual void doVirtR() = 0;
private:
void et(){
cout << "X::et" <<endl;
}
};
class K: public X {
public:
K(I * core):X(core) {
cout << "K::ctor " ;
}
virtual ~K() {
cout << "K::dtor ";
}
void doVirtR(){
cout << "doVirtK" <<endl;
}
};
class Y: public D {
public:
Y(I *core): D(core){
cout << "Y::ctor ";
}
virtual ~Y() {
cout << "Y::dtor ";
}
/*void et(){
cout << "Y::et" <<endl;
}*/
void do_it() {
D::do_it();
cout << 'Y';
}
void doY() {
cout << "doY" << endl;
}
protected:
virtual void doVirtS() = 0;
};
class L: public Y{
public:
L(I * core):Y(core) {
cout << "L::ctor ";
}
virtual ~L() {
cout << "L::dtor ";
}
void doVirtS(){
cout << "doVirtL" <<endl;
}
};
class Z: public D {
public:
Z(I *core): D(core){
cout << "Z::ctor ";
}
virtual ~Z() {
cout << "Z::dtor ";
}
void et(){
cout << "Z::et" <<endl;
}
void do_it() {
D::do_it();
cout << 'Z';
}
void doZ() {
cout << "doZ" << endl;
}
virtual void doVirtT() = 0;
};
class M: public Z{
public:
M(I * core):Z(core) { //must add D(core) here explicitly because of virtual inheritance in M's base class (Z).
cout << "M::ctor " ;
}
virtual ~M() {
cout << "M::dtor ";
}
void doVirtT(){
cout << "doVirtM" <<endl;
}
};
int main(void) //testing dynamic casting
{
I * inKLM = new L( new M( new K( new A )));
L * LinKLM = dynamic_cast<L *>( inKLM);
M * MinKLM = dynamic_cast<M *>( inKLM);
K * KinKLM = dynamic_cast<K *>( inKLM);
cout << endl;
if ( ! MinKLM ) cout << "null MinKLM!" << endl;
if ( ! LinKLM ) cout << "null LinKLM!" << endl;
if ( ! KinKLM ) cout << "null KinKLM!" << endl;
//KinKLM->doVirtR();
//LinKLM->doVirtS();
//MinKLM->doVirtT();
//LinKLM->D::train();
//KinKLM->do_it();
//MinKLM->doZ();
delete inKLM;
cout << endl;
return 0;
}
If you need access to functionality that is unique in some of the inner classes, you may be better off (depending on the particular problem) trying mixin classes. The basic idea is to have a template class inherit its template parameter. I have simplified the classes below but the principle is clear:
#include <iostream>
// your base class
class I {
public:
virtual void do_it() {}
};
// a decorator
template <class Base>
class T1 : public Base {
public:
void do_it() {
std::cout << "T1" << std::endl;
Base::do_it();
}
void unique_in_T1() {
std::cout << "Unique in T1" << std::endl;
}
};
// another decorator
template <class Base>
class T2 : public Base {
public:
void do_it() {
std::cout << "T2" << std::endl;
Base::do_it();
}
void unique_in_T2() {
std::cout << "Unique in T2" << std::endl;
}
};
// yet another decorator
template <class Base>
class T3 : public Base {
public:
void do_it() {
std::cout << "T3" << std::endl;
Base::do_it();
}
void unique_in_T3() {
std::cout << "Unique in T3" << std::endl;
}
};
int main(int argc, const char * argv[]) {
T3<T2<T1<I>>> my_object1;
my_object1.do_it();
my_object1.unique_in_T2();
T1<T3<I>> my_object2;
my_object2.do_it();
my_object2.unique_in_T3();
return 0;
}
Your class D is not needed anymore. The main purpose of that class is to wrap the object that actually does the job while maintaining the interface of I. With mixin classes there is no wrapping anymore as it has been replaced by inheritance, hence there is no need for the D class.
Here is a link to read more.
I have a state machine sort of thing encapsulated in a class. This is done by calling a sequence of member functions in turn to do the actions. I also have several derived classes, and I want to do the same sequence on them. Is there a way I can template this code?
#include <iostream>
class MyClass {
std::string name;
public:
typedef void (MyClass::*Function)(std::ostream &ostr);
void Func1(std::ostream &ostr) { ostr << "F1" << name << std::endl; };
void Func2(std::ostream &ostr) { ostr << "F2" << std::endl; };
static Function printers[];
void Print(std::ostream &ostr);
};
MyClass::Function MyClass::printers[] = {
&MyClass::Func1,
&MyClass::Func2,
NULL
};
void MyClass::Print(std::ostream &ostr)
{
// various stuff to do before
// ...
int i = 0;
for (Function *fp = printers; *fp; fp++, i++) {
std::cerr << "Calling function " << i << std::endl;
((this)->*(*fp))(ostr);
std::cerr << "Called function " << i << std::endl;
}
// other stuff here...
}
class DerClass: public MyClass {
int index;
public:
typedef void (DerClass::*Function)(std::ostream &ostr);
void Func3(std::ostream &ostr) { ostr << "F3" << index << std::endl; };
static Function printers[];
void Print(std::ostream &ostr);
};
DerClass::Function DerClass::printers[] = {
&DerClass::Func1,
&DerClass::Func3,
NULL
};
// I'd rather not repeat all this stuff, can I template it??
void DerClass::Print(std::ostream &ostr)
{
// various stuff to do before
// ...
int i = 0;
for (Function *fp = printers; *fp; fp++, i++) {
std::cerr << "Calling function " << i << std::endl;
((this)->*(*fp))(ostr);
std::cerr << "Called function " << i << std::endl;
}
// other stuff here...
}
int main()
{
MyClass cl1;
cl1.Print(std::cout);
DerClass cl2;
cl2.Print(std::cout);
}
The code runs, and works, but I'd like to write the Print routine just once, rather than for each new class, is there a way to do that, templates? Other classes?
I was hoping for something like
template <class T>
void T::Print(std::ostream &str) {
int i = 0;
for (Function *fp = printers; *fp; fp++, i++) {
std::cerr << "Calling function " << i << std::endl;
((this)->*(*fp))(ostr);
std::cerr << "Called function " << i << std::endl;
}
}
That doesn't compile though.
error: invalid use of template type parameter âTâ void
T::Print(std::ostream &str) {
I would rather write this as a comment, but the amount of code wouldn't be readable at all.
Below you can find a possible solution on how to "inherit" your Print.
This should only be taken as suggestion or hint. Especially the initialization of the printers is not really done in "a good way".
Hope it helps you nonetheless
#include <iostream>
#include <string>
template <class Derived>
class MyClass {
std::string name;
protected:
typedef void (Derived::*Function)(std::ostream &ostr);
Function* printers;
public:
MyClass()
:printers(nullptr)
{
}
~MyClass()
{
delete[] printers;
}
virtual void initPrinters()
{
if (printers == nullptr)
{
printers = new Function[3];
printers[0] = &MyClass<Derived>::Func1;
printers[1] = &MyClass<Derived>::Func2;
printers[2] = NULL;
}
}
void destroyPrinters()
{
if (printers != nullptr)
{
delete[] printers;
}
}
void Print(std::ostream &ostr)
{
initPrinters();
// various stuff to do before
// ...
int i = 0;
for (Function *fp = printers; *fp; fp++, i++) {
std::cerr << "Calling function " << i << std::endl;
(((Derived*)this)->*(*fp))(ostr);
std::cerr << "Called function " << i << std::endl;
}
// other stuff here...
}
void Func1(std::ostream &ostr) { ostr << "F1" << name << std::endl; };
void Func2(std::ostream &ostr) { ostr << "F2" << std::endl; };
};
template <>
class MyClass<void> : public MyClass<MyClass<void>>
{
};
class DerClass : public MyClass<DerClass> {
int index;
public:
virtual void initPrinters() override
{
if (printers == nullptr)
{
printers = new Function[3];
printers[0] = &DerClass::Func1;
printers[1] = &DerClass::Func3;
printers[2] = NULL;
}
}
void Func3(std::ostream &ostr) { ostr << "F3" << index << std::endl; };
};
int main()
{
MyClass<void> cl1;
cl1.Print(std::cout);
DerClass cl2;
cl2.Print(std::cout);
}
EDIT: Second Approach
This one is prettier in my opinion. Also it is less code and it can be optimized by the compiler:
#include <iostream>
#include <string>
class MyClass {
std::string name;
protected:
template <class T>
void print(std::ostream &ostr, void(T::*printFunc)(std::ostream&))
{
(((T*)this)->*printFunc)(ostr);
}
template <class T, typename... PrintFunctions>
void print(std::ostream &ostr, void(T::*printFunc)(std::ostream&), PrintFunctions... printFuncs)
{
(((T*)this)->*printFunc)(ostr);
print(ostr, printFuncs...);
}
public:
virtual void Print(std::ostream &ostr)
{
print(ostr, &MyClass::Func1, &MyClass::Func2);
}
void Func1(std::ostream &ostr) { ostr << "F1" << name << std::endl; };
void Func2(std::ostream &ostr) { ostr << "F2" << std::endl; };
};
class DerClass : public MyClass {
int index;
public:
virtual void Print(std::ostream &ostr) override
{
print(ostr, &DerClass::Func1, &DerClass::Func3);
}
void Func3(std::ostream &ostr) { ostr << "F3" << index << std::endl; };
};
int main()
{
MyClass cl1;
cl1.Print(std::cout);
DerClass cl2;
cl2.Print(std::cout);
}
If "various stuff" and "other stuff" are different, you can make those parts virtual.
But you can't call a member function of a subclass in the base class, the functions all need to have the same type.
One suggestion might be to indirect through free functions.
class MyClass;
typedef void (*Printer)(MyClass*, std::ostream&);
class MyClass {
std::string name;
public:
MyClass();
void Func1(std::ostream &ostr) { ostr << "F1" << name << std::endl; };
void Func2(std::ostream &ostr) { ostr << "F2" << std::endl; };
void Print(std::ostream &ostr);
protected:
MyClass(Printer* ps) : myprinters(ps) {}
virtual void PrePrint() { /* Various stuff... */ }
virtual void PostPrint() { /* Other stuff... */ }
private:
Printer* myprinters;
};
void MyFunc1(MyClass* obj, std::ostream& os) { obj->Func1(os); }
void MyFunc2(MyClass* obj, std::ostream& os) { obj->Func2(os); }
Printer myclassprinters[] = {
&MyFunc1,
&MyFunc2,
NULL
};
MyClass::MyClass()
: myprinters(myclassprinters)
{
}
void MyClass::Print(std::ostream &ostr)
{
PrePrint();
int i = 0;
for (Printer *fp = myprinters; *fp; fp++, i++) {
std::cerr << "Calling function " << i << std::endl;
(*fp)(this, ostr);
std::cerr << "Called function " << i << std::endl;
}
PostPrint();
}
class DerClass: public MyClass {
public:
DerClass();
void Func3(std::ostream &ostr) { ostr << "F3" << index << std::endl; };
protected:
void PrePrint() { /* More stuff... */ }
};
// C-cast for conciseness only. Use something safer in the real world.
void DerFunc(MyClass* obj, std::ostream& ostr) { ((DerClass*)obj)->Func3(ostr); }
Printer derclassprinters[] = {
&MyFunc1,
&DerFunc,
NULL
};
DerClass::DerClass()
: MyClass(derclassprinters)
{
}
This uses a pointer member for each instance - I'm assuming you don't want to store the entire function table for every instance.