operator=返回一个reference to *this

在C++中,调用拷贝构造函数有三种情况:

1.一个对象作为函数参数,以值传递的方式传入函数体

2.一个对象作为函数返回值,以值传递的方式从函数返回

3.一个对象用于给另外一个对象进行初始化(复制初始化).

拷贝构造函数必须以引用的形式传递(参数为引用值).其原因如下:

当一个对象以传递值的方式传一个函数的时候,拷贝构造函数自动的调用来生成函数中的对象.

这样会导致无限循环地调用拷贝构造函数,直至栈溢出.

以前,一直有个误解,以为以同类型的对象调用"="时,就会调用赋值符.参看以下的例子:


 1 class CTest {
 2 public:
 3     CTest();
 4     CTest(const CTest&);
 5     CTest& operator=(const CTest &);
 6 };
 7 CTest::CTest()
 8 {
 9     cout<<"Constructor of CTest"<<endl;
10 }
11 CTest::CTest(const CTest& arg)
12 {
13     cout<<"Copy Constructor of CTest"<<endl;
14 }
15 CTest& CTest::operator=(const CTest& arg)
16 {
17     cout<<"Assign function of CTest"<<endl;
18 }
19 int main()
20 {
21     CTest a;
22     CTest b(a);
23     CTest c = a;
24     a = c;
25     return 0;
26 }

按照以前的理解,第21~24行代码,应该分别调用构造函数,拷贝构造函数,赋值符函数,赋值符函数.

然而最终如下,不是如自己所想...说明以前的理解是错误的.

Constructor of CTest
Copy Constructor of CTest
Copy Constructor of CTest
Assign function of CTest

第23行代码调用的是拷贝构造函数,不是赋值符函数,但第24行代码调用的赋值符函数,不是拷贝构造函数.原因如下:

拷贝构造函数创建新的对象,而赋值符函数不创建新对象,它要求"="的左右对象均已存在,它的作用就是把"="右边的对象的值赋给左边的对象.

虽然编译器会提供拷贝构造函数和赋值符函数,但是有时候编译器提供的这些函数,并不能满足我们的需求,因而需要自定义拷贝构造函数和赋值函数.

这里就会引出一个新问题,什么时候需要自定义拷贝构造函数和赋值符函数.

简单的规则:如果需要定义一个非空的析构函数,那么,通常情况下也需要定义一个拷贝构造函数和赋值符函数.

通常的原则是:

1.对于凡是包含动态分配成员或包含指针成员的类都应该提供拷贝构造函数;

2.在提供拷贝构造函数的同时,还应该考虑重载"="赋值操作符,即提供赋值符函数.

当我们知道需要自定义拷贝构造函数和赋值符函数时,就得考虑如何良好的实现它们.

当自定义copying函数(包含拷贝构造函数和赋值符函数)时,需要确保以下两点:

1.复制所有的local成员变量

2.调用所有base classes内的适当的copying函数,完成基类的copying.

下面是一个具体的例子:


 1 void logcall(const std::string& funcName);    //制造一个log entry
 2 class Customer {
 3 public:
 4     ...
 5     Customer(const Customer& rhs);
 6     Customer& operator=(const Customer& rhs);
 7     ...
 8 private:
 9     std::string name;
10 };
11 Customer::Customer(const Customer& rhs):name(rhs.name)
12 {
13     logCall("Customer copy constructor");
14 }
15 Customer& Customer::operator=(const Customer& rhs)
16 {
17     logCall("Customer copy assignment operator");
18     name = rhs.name;                    //疑惑,为什么在copying函数里可以通过对象调用私有变量?
19     return *this;
20 }
21 
22 class PriorityCustomer:public Customer {
23 public:    
24     ...
25     PriorityCustomer(const PriorityCustomer& rhs);
26     PriorityCustomer& operator=(const PriorityCustomer& rhs);
27     ...
28 private:
29     int priority;
30 };
31 PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs):Customer(rhs),priority(rhs.priority)
32 {
33     logCall("PriorityCustomer copy constructor");
34 }
35 PriorityCustomer& PriorityCustomer::operator=(const PriorityCustomer& rhs)
36 {
37     logCall("PriorityCustomer copy assignment operator");
38     Customer::operator=(rhs);                //对base class成分进行赋值动作
39     priority = rhs.priority;
40     return *this;
41 }




1. 函数返回值和引用的区别

int val;

int GetValue() { return val; }    

int GetReference() {return val;}

int result = GetValue();    // 系统产生返回值副本temp, temp = GetValue(); result = temp; 

int &result = GetReference();    // 系统不产生返回值副本,直接返回全部变量 val 的引用

注:

函数返回值和返回引用的区别在于系统是否产生一个返回值的副本


2. a=(b=c) 实例分析 (a.operate=(b.opeare=(c)))

(1) operate= 返回值

     b=c;    //  返回一个临时变量temp1,temp1 通过 copy 构造函数被初始化(拷贝构造函数调用第一次)

     a=temp1;    //  返回一个临时变量temp2,temp2 通过 copy 构造函数被初始化(拷贝构造函数调用第二次)

(2) operate= 返回引用

    b=c;    //  返回对象 b 的引用 ref_b, 不产生返回值副本,不调用拷贝构造函数

    a = ref_b;    //  返回对象 a 的引用 ref_a, 不生成返回值副本,不调用拷贝构造函数

注:使用引用作为返回值的效率优于使用值。


3. (a=b)=c 实例分析

1) operate= 返回值

     a=b;    //  返回一个临时变量temp1,temp1 通过 copy 构造函数被初始化(拷贝构造函数调用第一次)

     temp1=c;    //  返回一个临时变量temp2,temp2 通过 copy 构造函数被初始化(拷贝构造函数调用第二次)

     注:这里最终只是给 temp1 进行了赋值,而 a 并未如愿成为 c 的值得,即表达式的初衷并未实现。

(2) operate= 返回引用

    a=b;    //  返回对象 a 的引用 ref_a, 不产生返回值副本,不调用拷贝构造函数

    ref_a = c;    //  返回对象 a 的引用 ref_a1, 不生成返回值副本,不调用拷贝构造函数

    注:引用是不可再被赋值的,最后一句ref_a 不是成为了 c 的引用, 而是通过ref_a 的这个引用,是 a 的值修改为c的值,改变的是a的内容。

template <typename T> class sptr { friend class wptr<T>; public: sptr(); ~sptr(); /** * @brief Constructor with specified object to be managed. * * @note Null sptr will be created if `other` is a `nullptr`. * @param other Object to be managed by wptr. */ sptr(T *other); /** * @brief Copy Constructor for sptr with different managed class type(T). * * @param other Input sptr object. */ sptr(const sptr<T> &other); /** * @brief Move constructor. * * @note `other` will be set to a null sptr. * @param other Input sptr object. */ sptr(sptr<T> &&other); /** * @brief Move assignment operator. * * @param other Input sptr object. * @note Original strong reference in target sptr object will be removed. */ sptr<T> &operator=(sptr<T> &&other); /** * @brief Copy Constructor for sptr with different managed class type(O). * * @tparam O Another specific class type managed by `other`. * @param other Input sptr object. */ template <typename O> sptr(const sptr<O> &other); /** * @brief Constructor only used in promote process of wptr. * * @param p WeakRefCounter object which hold the reference to the * managed object. * @param force Only used to identify from other constructor. */ inline sptr(WeakRefCounter *p, bool force); /** * @brief Get the pointer to the managed object. * * @return Pointer of the specific managed class type. */ inline T *GetRefPtr() const { return refs_; } /** * @brief Set the pointer to the managed object. * * @param other Another pointer object to be managed by sptr. * @note Avoid using independently, otherwise it will * cause mismatch of reference count and thus memory problems. */ inline void ForceSetRefPtr(T *other); /** * @brief Remove the reference to the managed object held by current sptr. * * @note It will make this sptr a "null sptr". */ void clear(); /** * @brief Type conversion operator. * * @return Raw pointer to the managed object. * @note Sptr object itself will not be converted, only the member raw * pointer returns. */ inline operator T *() const { return refs_; } /** * @brief Dereference operator. * * It will return the object managed by this sptr. * * @return Return reference of specific object managed by sptr. */ inline T &operator*() const { return *refs_; } /** * @brief Member selection operator. * * It will return the specified member of the object managed by this sptr. */ inline T *operator->() const { return refs_; } /** * @brief Logical-NOT operator. Check if sptr is a "null sptr". * * @return Return true if sptr is a "null sptr". */ inline bool operator!() const { return refs_ == nullptr; } /** * @brief Copy assignment operator with specified object to be managed. * * @note Original reference will be removed, then new reference to the * input object will be established. * @param other Another object to be managed by this sptr. */ sptr<T> &operator=(T *other); /** * @brief Copy assignment operator for sptr with * same managed class type(T). * * @note Original reference will be removed, this sptr will manage the * same object with the input sptr object. * @param other Another sptr object with same managed class type(T). */ sptr<T> &operator=(const sptr<T> &other); /** * @brief Copy assignment operator for wptr with * same managed class type(T). * * @note Original reference will be removed, this sptr will manage the * same object with the input wptr object. * @note This may fail, then this sptr will turn to be a "null sptr". * @param other Another wptr object with same managed class type(T). */ sptr<T> &operator=(const wptr<T> &other); /** * @brief Copy assignment operator for sptr with * different managed class type(O). * * @note Original reference will be removed, this sptr will manage the * same object with the input sptr object. * @note This sptr will interpret the managed object as a type T. * @param other Another sptr object with different managed class type(O). */ template <typename O> sptr<T> &operator=(const sptr<O> &other); /** * @brief Equal-to operator between sptr and a raw pointer. * * @param other Input raw pointer. * @return Return true if sptr point to the same object with input * raw pointer. */ bool operator==(const T *other) const; /** * @brief Not-equal-to operator between sptr and a raw pointer. * * @param other Input raw pointer. * @return Return true if sptr does not point to the same object with input * raw pointer. */ inline bool operator!=(const T *other) const { return !operator==(other); } /** * @brief Equal-to operator between sptr and a wptr. * * @param other Input wptr. * @return Return true if sptr and wptr are managing same object. */ bool operator==(const wptr<T> &other) const; /** * @brief Not-equal-to operator between sptr and a wptr. * * @param other Input wptr. * @return Return true if sptr and wptr are not managing same object. */ inline bool operator!=(const wptr<T> &other) const { return !operator==(other); } /** * @brief Equal-to operator between sptrs. * * @param other Input sptr. * @return Return true if two sptrs are managing same object. */ bool operator==(const sptr<T> &other) const; /** * @brief Not-equal-to operator between sptrs. * * @param other Input sptr. * @return Return true if two sptrs are not managing same object. */ inline bool operator!=(const sptr<T> &other) const { return !operator==(other); } private: T *refs_ = nullptr; // Raw pointer to the managed specific object }; template <typename T> inline void sptr<T>::ForceSetRefPtr(T *other) { refs_ = other; } template <typename T> inline sptr<T>::sptr() { refs_ = nullptr; } template <typename T> inline sptr<T>::sptr(T *other) { refs_ = other; if (refs_ != nullptr) { refs_->IncStrongRef(this); } } template <typename T> inline sptr<T>::sptr(const sptr<T> &other) { refs_ = other.GetRefPtr(); if (refs_ != nullptr) { refs_->IncStrongRef(this); } } template <typename T> sptr<T>::sptr(sptr<T> &&other) { refs_ = other.GetRefPtr(); other.ForceSetRefPtr(nullptr); } template <typename T> sptr<T> &sptr<T>::operator=(sptr<T> &&other) { if (refs_ != nullptr) { refs_->DecStrongRef(this); } refs_ = other.GetRefPtr(); other.ForceSetRefPtr(nullptr); return *this; } template <typename T> template <typename O> sptr<T>::sptr(const sptr<O> &other) : refs_(other.GetRefPtr()) { if (refs_ != nullptr) { refs_->IncStrongRef(this); } } template <typename T> inline sptr<T> &sptr<T>::operator=(T *other) { if (other != nullptr) { other->IncStrongRef(this); } if (refs_ != nullptr) { refs_->DecStrongRef(this); } refs_ = other; return *this; } template <typename T> inline sptr<T> &sptr<T>::operator=(const sptr<T> &other) { T *otherRef(other.GetRefPtr()); if (otherRef != nullptr) { otherRef->IncStrongRef(this); } if (refs_ != nullptr) { refs_->DecStrongRef(this); } refs_ = otherRef; return *this; } template <typename T> inline sptr<T> &sptr<T>::operator=(const wptr<T> &other) { if (refs_ != nullptr) { refs_->DecStrongRef(this); } if ((other != nullptr) && other.AttemptIncStrongRef(this)) { refs_ = other.GetRefPtr(); } else { refs_ = nullptr; } return *this; } template <typename T> template <typename O> sptr<T> &sptr<T>::operator=(const sptr<O> &other) { T *otherRef(other.GetRefPtr()); if (otherRef != nullptr) { otherRef->IncStrongRef(this); } if (refs_ != nullptr) { refs_->DecStrongRef(this); } refs_ = otherRef; return *this; } template <typename T> inline bool sptr<T>::operator==(const T *other) const { return other == refs_; } template <typename T> inline bool sptr<T>::operator==(const wptr<T> &other) const { return refs_ == other.GetRefPtr(); } template <typename T> inline bool sptr<T>::operator==(const sptr<T> &other) const { return refs_ == other.GetRefPtr(); } template<typename T> void sptr<T>::clear() { if (refs_) { refs_->DecStrongRef(this); refs_ = 0; } } template <typename T> inline sptr<T>::~sptr() { if (refs_ != nullptr) { refs_->DecStrongRef(this); } } template <typename T> inline sptr<T>::sptr(WeakRefCounter *p, bool /* force */) { if ((p != nullptr) && p->AttemptIncStrongRef(this)) { refs_ = reinterpret_cast<T *>(p->GetRefPtr()); } else { refs_ = nullptr; } }
最新发布
10-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值