我試圖使用虛擬方法表的索引 類調用函數...假設我們有以下代碼:
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;
};
我已經實現了這個使用樂趣ction數組,if語句 和case-statement ...所以,是否有更好的方法來調用方法 只使用指針(例如訪問vtable)或類似的東西?
決心在此之後我要去創建派生類(例如derived1和衍生2) 與F_1,F_2,F_3的不同的實現並具有一類控制是這樣的:
class Control
{
protected:
Base* current;
public:
Control(Base* curr = new derived1): current(curr) {}
virtual ~Control()
{
delete current;
}
virtual void call_functions(unsigned int func_numb)
{
delete current
Base* new = current->call_func(func_numb);
current = new;
}
};
對不起我可怕的英語:S ...並提前致謝!
但是爲什麼?有意義的名字有一些東西。 –
這是一種功課嗎?這個問題與[幾分鐘前發佈的內容]有着驚人的相似性(http://stackoverflow.com/questions/13841248/c-accessing-virtual-methods) – juanchopanza