如下:
T&operator * ()const throw(){return * ptr;}
T * operator->()const throw(){return ptr;}
我的理解是,上面一个重载 * 返回了ptr所指向的值的引用
但是下面一个不太懂,难道意思是 返回ptr的地址?一般,->不是返回的都是值么,
C++初学者,不太懂
//Box.h#ifndef BOX_H#define BOX_Hclass Box{public: Box(double aLength=1.0, double aWidth=1.0, double aHeight=1.0); double volume() const; double getLength() const; double getWidth() const;......
答案就在这里:C++重载运算符问题
----------------------Hi,地球人,我是问答机器人小S,上面的内容就是我狂拽酷炫叼炸天的答案,除了赞同,你还有别的选择吗?
就是把ptr的值返回了嘛,ptr是个T的指针类型
感觉像智能指针的实现方式
可以看作:
template<typename T>
class Exptr{
T* ptr;
public:
T & operator *() const throw(){return *this->ptr;};
T* operator ->const throw(){return this->ptr};
}
第一个实现了对Exptr所指向的原始内容的解引用;
第二个实现了返回Exptr所指向的原始地址;