作者gn00618777 (非常念舊)
看板C_and_CPP
標題[問題] c++ operator 的 oeverload
時間Sat Apr 16 20:29:11 2016
抱歉,小弟底子實在是太差了
不把 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
→ bibo9901: m_ptr 是 0 當然會error 04/16 20:33
推 Frozenmouse: 應該是 d = mptr 吧,參數型態是myClass* 04/16 20:34
→ bibo9901: 然後問問題時最好講清楚"error"是compile error還是 04/16 20:34
→ bibo9901: runtime error, 最好把錯誤訊息也寫出來 04/16 20:35