请问这段代码为何这么写

wangjs720 2009-11-05 05:48:40
template <class U>
RefPtr(const RefPtr<U> & other) : m_ptr(other.get()) {
if (T * ptr = m_ptr) {
ptr->addRef();
}
}

这段代码为什么不这样写:

template <class U>
RefPtr(const RefPtr<U> & other) : m_ptr(other.get()) {
if (m_ptr != 0) {
m_ptr->addRef();
}
}

第一种写法多了一个赋值操作,为什么要这么写呢?
...全文
237 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
m_ptr是什么类型?跟上下文有关吧。
sj13426074890 2009-11-06
  • 打赏
  • 举报
回复
mark 一下
wfx_net 2009-11-06
  • 打赏
  • 举报
回复
00001 // -*- mode: c++; c-basic-offset: 4 -*-
00002 /*
00003 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
00004 *
00005 * This library is free software; you can redistribute it and/or
00006 * modify it under the terms of the GNU Library General Public
00007 * License as published by the Free Software Foundation; either
00008 * version 2 of the License, or (at your option) any later version.
00009 *
00010 * This library is distributed in the hope that it will be useful,
00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00013 * Library General Public License for more details.
00014 *
00015 * You should have received a copy of the GNU Library General Public License
00016 * along with this library; see the file COPYING.LIB. If not, write to
00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018 * Boston, MA 02110-1301, USA.
00019 *
00020 */
00021
00022 #ifndef WTF_RefPtr_h
00023 #define WTF_RefPtr_h
00024
00025 #include <algorithm>
00026 #include "AlwaysInline.h"
00027
00028 namespace WTF {
00029
00030 enum PlacementNewAdoptType { PlacementNewAdopt };
00031
00032 template <typename T> class PassRefPtr;
00033
00034 enum HashTableDeletedValueType { HashTableDeletedValue };
00035
00036 template <typename T> class RefPtr {
00037 public:
00038 RefPtr() : m_ptr(0) { }
00039 RefPtr(T* ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); }
00040 RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { if (T* ptr = m_ptr) ptr->ref(); }
00041 // see comment in PassRefPtr.h for why this takes const reference
00042 template <typename U> RefPtr(const PassRefPtr<U>&);
00043
00044 // Special constructor for cases where we overwrite an object in place.
00045 RefPtr(PlacementNewAdoptType) { }
00046
00047 // Hash table deleted values, which are only constructed and never copied or destroyed.
00048 RefPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { }
00049 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); }
00050
00051 ~RefPtr() { if (T* ptr = m_ptr) ptr->deref(); }
00052
00053 template <typename U> RefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { if (T* ptr = m_ptr) ptr->ref(); }
00054
00055 T* get() const { return m_ptr; }
00056
00057 void clear() { if (T* ptr = m_ptr) ptr->deref(); m_ptr = 0; }
00058 PassRefPtr<T> release() { PassRefPtr<T> tmp = adoptRef(m_ptr); m_ptr = 0; return tmp; }
00059
00060 T& operator*() const { return *m_ptr; }
00061 ALWAYS_INLINE T* operator->() const { return m_ptr; }
00062
00063 bool operator!() const { return !m_ptr; }
00064
00065 // This conversion operator allows implicit conversion to bool but not to other integer types.
00066 typedef T* RefPtr::*UnspecifiedBoolType;
00067 operator UnspecifiedBoolType() const { return m_ptr ? &RefPtr::m_ptr : 0; }
00068
00069 RefPtr& operator=(const RefPtr&);
00070 RefPtr& operator=(T*);
00071 RefPtr& operator=(const PassRefPtr<T>&);
00072 template <typename U> RefPtr& operator=(const RefPtr<U>&);
00073 template <typename U> RefPtr& operator=(const PassRefPtr<U>&);
00074
00075 void swap(RefPtr&);
00076
00077 private:
00078 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); }
00079
00080 T* m_ptr;
00081 };
00082
00083 template <typename T> template <typename U> inline RefPtr<T>::RefPtr(const PassRefPtr<U>& o)
00084 : m_ptr(o.releaseRef())
00085 {
00086 }
00087
00088 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<T>& o)
00089 {
00090 T* optr = o.get();
00091 if (optr)
00092 optr->ref();
00093 T* ptr = m_ptr;
00094 m_ptr = optr;
00095 if (ptr)
00096 ptr->deref();
00097 return *this;
00098 }
00099
00100 template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<U>& o)
00101 {
00102 T* optr = o.get();
00103 if (optr)
00104 optr->ref();
00105 T* ptr = m_ptr;
00106 m_ptr = optr;
00107 if (ptr)
00108 ptr->deref();
00109 return *this;
00110 }
00111
00112 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(T* optr)
00113 {
00114 if (optr)
00115 optr->ref();
00116 T* ptr = m_ptr;
00117 m_ptr = optr;
00118 if (ptr)
00119 ptr->deref();
00120 return *this;
00121 }
00122
00123 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<T>& o)
00124 {
00125 T* ptr = m_ptr;
00126 m_ptr = o.releaseRef();
00127 if (ptr)
00128 ptr->deref();
00129 return *this;
00130 }
00131
00132 template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<U>& o)
00133 {
00134 T* ptr = m_ptr;
00135 m_ptr = o.releaseRef();
00136 if (ptr)
00137 ptr->deref();
00138 return *this;
00139 }
00140
00141 template <class T> inline void RefPtr<T>::swap(RefPtr<T>& o)
00142 {
00143 std::swap(m_ptr, o.m_ptr);
00144 }
00145
00146 template <class T> inline void swap(RefPtr<T>& a, RefPtr<T>& b)
00147 {
00148 a.swap(b);
00149 }
00150
00151 template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, const RefPtr<U>& b)
00152 {
00153 return a.get() == b.get();
00154 }
00155
00156 template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, U* b)
00157 {
00158 return a.get() == b;
00159 }
00160
00161 template <typename T, typename U> inline bool operator==(T* a, const RefPtr<U>& b)
00162 {
00163 return a == b.get();
00164 }
00165
00166 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const RefPtr<U>& b)
00167 {
00168 return a.get() != b.get();
00169 }
00170
00171 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, U* b)
00172 {
00173 return a.get() != b;
00174 }
00175
00176 template <typename T, typename U> inline bool operator!=(T* a, const RefPtr<U>& b)
00177 {
00178 return a != b.get();
00179 }
00180
00181 template <typename T, typename U> inline RefPtr<T> static_pointer_cast(const RefPtr<U>& p)
00182 {
00183 return RefPtr<T>(static_cast<T*>(p.get()));
00184 }
00185
00186 template <typename T, typename U> inline RefPtr<T> const_pointer_cast(const RefPtr<U>& p)
00187 {
00188 return RefPtr<T>(const_cast<T*>(p.get()));
00189 }
00190
00191 template <typename T> inline T* getPtr(const RefPtr<T>& p)
00192 {
00193 return p.get();
00194 }
00195
00196 } // namespace WTF
00197
00198 using WTF::RefPtr;
00199 using WTF::static_pointer_cast;
00200 using WTF::const_pointer_cast;
00201
00202 #endif // WTF_RefPtr_h
se7venhigh 2009-11-05
  • 打赏
  • 举报
回复
感觉想要保护私有成员m_ptr
wangjs720 2009-11-05
  • 打赏
  • 举报
回复
有很多地址都是这么使用的:
~RefPtr() { if (T* ptr = m_ptr) ptr->deref(); }
wangjs720 2009-11-05
  • 打赏
  • 举报
回复
这个类在这儿:
http://api.kde.org/4.x-api/kdelibs-apidocs/kjs/wtf/html/RefPtr_8h_source.html
老吴笔记 2009-11-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 jackyjkchen 的回复:]
和上下文有关系么,把这个类贴一下
[/Quote]
UP
jackyjkchen 2009-11-05
  • 打赏
  • 举报
回复
和上下文有关系么,把这个类贴一下
lori227 2009-11-05
  • 打赏
  • 举报
回复
addRef 函数如果是子类的, 父类调用就有问题的 所以转一下...
lori227 2009-11-05
  • 打赏
  • 举报
回复
m_ptr 是U* 类型, 要转换成T*类型调用addRef函数

65,185

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧