More effective c++解引操作符及指针(30)

智能指针解引操作符:++
1
2
3
4
5
6
7
Template<Class T>
T&SmartPtr<T>::operator*() const {
……….
Return * pointee;
}
DBPtr<Tuple>&pt;
Pt->dispalyEditDialog();

注意编译器解释为:

(pt.operator->())->displayEditDialog();

所以 operator->可能返回俩种东西,一个对象,一个智能指针,要注意分清。

指针判断

If (ptn == 0)
If (ptn)

都是错误的,应该向类提供隐式转换才会被认同。

添加:operator void*();
但有缺陷,在指针比较时if (pa == po)可以通过

所以,目的只是为了检索指针是否为空,直接重载否定符,只使用为空部分:

bool operator !() const{};

这样if (!ptn)就没问题了

对于自己的编码需求给智能指针添加隐式转换

operator T*() {return pointee;}

但是智能指针的隐式转换有臭虫漏洞!
注意,详细看172.
最好去掉隐式转换。自己封装函数来获取。

// //