[問題] c++ operator 的 oeverload
抱歉,小弟底子實在是太差了
不把 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
04/16 20:33, 1F
推
04/16 20:34, , 2F
04/16 20:34, 2F
→
04/16 20:34, , 3F
04/16 20:34, 3F
→
04/16 20:35, , 4F
04/16 20:35, 4F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章