如果不抽象,則調用基類方法。如果不抽象,調用基類方法
class WithAbstMethod {
public:
virtual void do() = 0;
}
class WithImplMethod : public WithAbstMethod {
public:
virtual void do() {
// do something
}
}
template<typename BaseT>
class DerivedClass : BaseT {
public:
virtual void do() {
BaseT::do(); // here is a question. How to modify code, so that do() is called if it is not abstract?
// do something
}
}
void main() {
DerivedClass<WithAbstMethod> d1;
d1.do(); // only DerivedClass::do() should be called
DerivedClass<WithImplMethod> d2;
d2.do(); // both WithImplMethod::do() and DerivedClass::do() should be called
}
是否有可能做到這一點在編譯時使用模板沒有太多的代碼(實例DerivedClass :: DO()方法巴塞特:: DO()調用並且不依賴於巴塞特型)? 顯然,在WithAbstMethod類中提供實現不是一個選項。上面的代碼是僞代碼,因此可能包含小錯誤。
請注意'do'是C++中的保留字:) – 2012-01-31 23:49:40