[問題] c++ operator 的 oeverload

看板C_and_CPP (C/C++)作者 (非常念舊)時間10年前 (2016/04/16 20:29), 編輯推噓1(103)
留言4則, 2人參與, 最新討論串1/2 (看更多)
抱歉,小弟底子實在是太差了 不把 template 和 operator 的 overload 搞定根本很難看 android 的 code... #include<iostream> #include<stdlib.h> using namespace std; template<typename T> class Demo{ public: inline Demo():m_ptr(0){ cout<<"This is the First constructor "<<endl; } Demo(T* other); Demo(const Demo<T>& other); //Assigment Demo& operator = (T* other); //Accessors inline T* operator-> () const { return m_ptr; } inline T& operator* () const { return *m_ptr; } private: T* m_ptr; }; template<typename T> Demo<T>::Demo(T* other){ cout<<"This is the Second constructor"<<endl; } template<typename T> Demo<T>::Demo(const Demo<T>& other){ cout<<"This is the Thirdth constructor"<<endl; } class myClass{ public: myClass(); }; int main(){ Demo<myClass> d; myClass *mptr = 0; //我該如何使用 Demo 類別的 Assigment 和 Accessors 呢? cout << *d <<endl; //error d = *mptr; //error d.operator*(mptr); //error } 感謝指教... -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 58.115.110.72 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1460809755.A.8C6.html

04/16 20:33, , 1F
m_ptr 是 0 當然會error
04/16 20:33, 1F

04/16 20:34, , 2F
應該是 d = mptr 吧,參數型態是myClass*
04/16 20:34, 2F

04/16 20:34, , 3F
然後問問題時最好講清楚"error"是compile error還是
04/16 20:34, 3F

04/16 20:35, , 4F
runtime error, 最好把錯誤訊息也寫出來
04/16 20:35, 4F
文章代碼(AID): #1N4Z0RZ6 (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1N4Z0RZ6 (C_and_CPP)