[問題] Call child class function from parent

看板C_and_CPP作者 (gigigigi)時間8年前 (2017/09/23 09:43), 編輯推噓1(108)
留言9則, 3人參與, 最新討論串1/1
開發平台(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 子類型函數不行 請問一下這是什麼原因? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 112.104.30.124 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1506131006.A.505.html

09/23 10:03, , 1F
virtual void toImplement(T& t) = 0; // 找到原因了
09/23 10:03, 1F

09/23 10:03, , 2F
加上這行
09/23 10:03, 2F

09/23 19:19, , 3F
父 call 子本來就不是隨便能做的
09/23 19:19, 3F

09/23 19:19, , 4F
父親不會知道哪個子繼承自己需要我去 call
09/23 19:19, 4F

09/23 19:19, , 5F
如果真的需要用到父 call 子這種寫法的話可以去查 CRTP
09/23 19:19, 5F

09/23 19:20, , 6F
CRTP 利用了模版參數讓父知道是哪個子繼承自己
09/23 19:20, 6F

09/27 22:39, , 7F
CRTP...這會造成閱讀跟除錯困難 XD
09/27 22:39, 7F

09/27 22:40, , 8F
其實我私心認為 寫code寫成這樣 應該邏輯也有問題了...
09/27 22:40, 8F

09/27 22:40, , 9F
建議把整個code邏輯展開看一下為什麼非得這樣做
09/27 22:40, 9F
文章代碼(AID): #1PnRm-K5 (C_and_CPP)