我有以下类层次结构(实际上还有更多的类),我想知道是否可以重新组织以下内容以利用静态多态性?
struct return_val {};
struct base
{
virtual ~base(){}
virtual return_val work(){};
};
struct derivedtype1 : public base
{
return_val work() { return localwork(next_type.work()); }
return_val localwork(return_val& rv0){....}
base* next_type0;
};
struct derivedtype2 : public base
{
return_val work() { return localwork(next_type0.work(),next_type1.work()); }
return_val localwork(return_val& rv0, return_val& rv1){....}
base* next_type0;
base* next_type1;
};
struct derivedtype3 : public base
{
return_val work() { return localwork(next_type0.work(),next_type1.work(),next_type2.work()); }
return_val localwork(return_val& rv0, return_val& rv1, return_val& rv2){.....}
base* next_type0;
base* next_type1;
base* next_type2;
};
我问,在进行了大量的分析之后,虚拟方法调用的开销实际上是相当大的,并且希望尽可能地优化它。