[問題] Call child class function from parent

作者: gigigigi (gigigigi)   2017-09-23 09:43:23
開發平台(Platform): (Ex: Win10, Linux, ...)
Linux
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
g++
https://gist.github.com/anonymous/f67031c9f2c237589ca2cf4214c86e1a
error: cannot call member function ‘void Derived<T>::toImplement(T&) [with T
= double]’ without object Derived<T>::toImplement(t);
Base<T>::tryUsingImplemented(t); // 這邊從子類別函數 call 父類型函數ok
Derived<T>::toImplement(t); //但是從父類別函數 call 子類型函數不行
請問一下這是什麼原因?
謝謝
作者: gigigigi (gigigigi)   2017-09-23 10:03:00
virtual void toImplement(T& t) = 0; // 找到原因了加上這行
作者: LPH66 (-6.2598534e+18f)   2017-09-23 19:19:00
父 call 子本來就不是隨便能做的父親不會知道哪個子繼承自己需要我去 call如果真的需要用到父 call 子這種寫法的話可以去查 CRTPCRTP 利用了模版參數讓父知道是哪個子繼承自己
作者: Killercat (殺人貓™)   2017-09-27 22:39:00
CRTP...這會造成閱讀跟除錯困難 XD其實我私心認為 寫code寫成這樣 應該邏輯也有問題了...建議把整個code邏輯展開看一下為什麼非得這樣做

Links booklink

Contact Us: admin [ a t ] ucptt.com