I'm trying to use the virtual method table to call functions by index in a class... Suppose we have the following code:
class Base
{
public:
Base() {}
virtual ~Base() {}
virtual Base* call_func(unsigned int func_number)
{
// Some way to call f_n
}
protected:
virtual Base* f_1() const = 0;
virtual Base* f_2() const = 0;
virtual Base* f_3() const = 0;
};
I've already implemented this using function arrays, if-statement and case-statement... so, Is there a a better approach to call methods using just pointers (accessing to the vtable for example) or something like that?
Sorry for my horrible English :S... and thanks in advance!
EDIT: Thanks for all the suggestion! I'm going to expand my question:
After resolve this i'm going to create derived classes (for example derived1 and derived 2) with different implementations of f_1, f_2, f_3 and have a class control like this:
class Control
{
protected:
Base* current;
public:
Control(Base* curr = new derived1): current(curr) {}
virtual ~Control()
{
delete current;
}
virtual void do_something(unsigned int func_numb)
{
delete current
Base* new = current->next_state(stl);
current = new;
}
};
Either a switch statement:
switch (func_number)
{
case 1:
f_1();
break;
case 2:
f_2();
break;
case 3:
f_3();
break;
}
Or use an array of function pointers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With