[问题] c++ operator 的 oeverload

楼主: gn00618777 (非常念旧)   2016-04-16 20:29:11
抱歉,小弟底子实在是太差了
不把 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
}
感谢指教...
作者: bibo9901 (function(){})()   2016-04-16 20:33:00
m_ptr 是 0 当然会error
作者: Frozenmouse (*冰之鼠*)   2016-04-16 20:34:00
应该是 d = mptr 吧,参数型态是myClass*
作者: bibo9901 (function(){})()   2016-04-16 20:34:00
然后问问题时最好讲清楚"error"是compile error还是runtime error, 最好把错误讯息也写出来

Links booklink

Contact Us: admin [ a t ] ucptt.com