operator 中处理”自我赋值“

operator=操作符缺省情况下返回引用——TYPE& TYPE::operator=(const TYPE&),原因很简单,operator=返回引用的理由是使你能在一个语句中连接多个赋值。

int x, y, z;
x = y = z = ; // chain of assignments

赋值采用右结合律,上面的代码被编译器解释为:

x = (y = (z = ));

在编译过程中,赋值是右结合的。说白了就是如果你想要玩一下多个赋值,operator=返回的东西必须是右(rhs)赋值。除了返回对对象自身的引用还能有什么呢?这就是为什么operator=最后一行总是返回对this的引用:

class Widget {
public:
  ...
  Widget& operator=(const Widget& rhs) // return type is a reference to
  { // the current class
  ...
  return *this; // return the left-hand object
  }
  ...
};

再来看自我赋值,即对象被赋值给自己,假设建立一个class用来保存一个指针指向一块动态分配的bitmap

class Bitmap { ... };
class Widget {
  ...
  private:
  Bitmap *pb; // ptr to a heap-allocated object
};

下面是operator=代码,表面上看合理,但自我赋值时并不安全。

Widget&
Widget::operator=(const Widget& rhs) // unsafe impl. of operator=
{
  delete pb; // stop using current bitmap
  pb = new Bitmap(*rhs.pb); // start using a copy of rhs’s bitmap
  return *this; // see Item 10
}

如果rhs与*this指向同一对象,第一句的delete pb就销毁了bitmap中调用的rhs.pb,这一问题的解决方案是copy and swap:

class Widget {
...
void swap(Widget& rhs); // exchange *this’s and rhs’s data;
... // see Item29 for details
};
Widget& Widget::operator=(const Widget& rhs)
{
  Widget temp(rhs); // make a copy of rhs’s data
  swap(temp); // swap *this’s data with the copy’s
  return *this;
}

它先将rhs做一个复本,由这个复本与this交换数据来达到目的。

复制对象时确保复制对象内每个成员变量

类customer如下所示:

void logCall(const std::string& funcName); // make a log entry
class Customer {
public:
  ...
  Customer(const Customer& rhs);
  Customer& operator=(const Customer& rhs);
  ...
private:
  std::string name;
};
Customer::Customer(const Customer& rhs)
  : name(rhs.name) // copy rhs’s data
{
  logCall("Customer copy constructor");
}
Customer& Customer::operator=(const Customer& rhs)
{
  logCall("Customer copy assignment operator");
  name = rhs.name; // copy rhs’s data
  return *this; // see Item 10
}

这个程序完全正确,但当在类中加入一个成员变量,我们同时也要在复制运算中加入相应的变量复制。

class Date { ... }; // for dates in time
class Customer {
public:
  ... // as before
private:
  std::string name;
  Date lastTransaction;
};

如果忘记修改operator=函数,编译器不会提醒你,从而造成潜在的漏洞。

再来看派生类复制的情况

class PriorityCustomer: public Customer { // a derived class
public:
  ...
  PriorityCustomer(const PriorityCustomer& rhs);
  PriorityCustomer& operator=(const PriorityCustomer& rhs);
  ...
private:
  int priority;
};
PriorityCustomer::PriorityCustomer(const PriorityCustomer& rhs)
: priority(rhs.priority)
{
  logCall("PriorityCustomer copy constructor");
}
 PriorityCustomer&
PriorityCustomer::operator=(const PriorityCustomer& rhs)
{
  logCall("PriorityCustomer copy assignment operator");
  priority = rhs.priority;
  return *this;
}
PriorityCustomer好像是完全复制了成员变量,但每个PriorityCustomer类都还包含着基类的成员变量未被复制,PriorityCustomer类中的customer成分被不带实参的customer构造函数初始化,customer成员变量初始化为缺省的值。我们所要做的是提供一个第三方的函数来调用基类的复制函数。

注意两个错误用法:1、令copy assignment操作符调用copy构造函数是错误的,因为在这就像试图构造一个已存在的对象。2、令copy构造函数调用copy assignment操作符同样是错误的。构造函数用来出事后对象,而assignment操作符只实行与已初始化的对象身上。对一个尚未构造好的对象赋值,就像在一个尚未初始化的对象身上做“z只对已初始化对象才有意义”的事意义。

effective c++:对象的赋值运算的更多相关文章

  1. Effective C++ —— 构造/析构/赋值运算(二)

    条款05 : 了解C++默默编写并调用哪些函数 编译器可以暗自为class创建default构造函数.copy构造函数.copy assignment操作符,以及析构函数. 1. default构造函 ...

  2. Effective C++ -- 构造析构赋值运算

    05.了解C++默默编写并调用哪些函数 编译产生的析构函数时non-virtual,除非这个类的基类析构函数为virtual 成员变量中有引用和const成员时,无法自己主动生成copy assign ...

  3. 《Effective C++》第2章 构造/析构/赋值运算(2)-读书笔记

    章节回顾: <Effective C++>第1章 让自己习惯C++-读书笔记 <Effective C++>第2章 构造/析构/赋值运算(1)-读书笔记 <Effecti ...

  4. 《Effective C++》第2章 构造/析构/赋值运算(1)-读书笔记

    章节回顾: <Effective C++>第1章 让自己习惯C++-读书笔记 <Effective C++>第2章 构造/析构/赋值运算(1)-读书笔记 <Effecti ...

  5. Effective C++ 笔记二 构造/析构/赋值运算

    条款05:了解C++默默编写并调用哪些函数 编译器默认声明一个default构造函数.一个copy构造函数.一个copy assignment操作符和一个析构函数.这些函数都是public且inlin ...

  6. Effective C++: 02构造、析构、赋值运算

    05:了解C++默默编写并调用哪些函数 1:一个空类,如果你自己没声明,编译器就会为它声明(编译器版本的)一个copy构造函数.一个copy assignment操作符和一个析构函数.此外如果你没有声 ...

  7. C++中的构造函数,拷贝构造函数和赋值运算

    关于C++中的构造函数,拷贝构造函数和赋值运算,以前看过一篇<高质量C++/C编程指南>的文章中介绍的很清楚,网上能搜索到,如果想详细了解这方面的知识可以参看一下这篇文章. 常见的给对象赋 ...

  8. js赋值运算的理解

    简介 js引擎由于为了效率,很多时候的非直接量赋值都不是copy一份在赋值给新的变量,而是一个引用 ps:直接量:直接值数字字符串等 为什么使用len = doms.length; 里的len效率要比 ...

  9. Effective C++(11) 自我赋值(a=a)时会发生什么?

    问题聚焦: 自我赋值看似有点愚蠢的行为,其实总会发生的 首先:它是合法的, 其次,它不一定是安全的, 再次,它有时候不是那么明显. 先看一个Demo class Widget { ... }; Wid ...

随机推荐

  1. java.lang.NoSuchFieldError: VERSION_2_3_0 报错解决方案

    java.lang.NoSuchFieldError: VERSION_2_3_0 at org.apache.struts2.views.freemarker.FreemarkerManager.c ...

  2. poj -3262 Protecting the Flowers (贪心)

    http://poj.org/problem?id=3262 开始一直是理解错题意了!!导致不停wa. 这题是农夫有n头牛在花园里啃花朵,然后农夫要把它们赶回棚子,每次只能赶一头牛,并且给出赶回每头牛 ...

  3. [POJ3264]Balanced Lineup(RMQ, ST算法)

    题目链接:http://poj.org/problem?id=3264 典型RMQ,这道题被我鞭尸了三遍也是醉了…这回用新学的st算法. st算法本身是一个区间dp,利用的性质就是相邻两个区间的最值的 ...

  4. 浅析Java web程序之客户端和服务器端交互原理(转)

    转载自http://www.cnblogs.com/lys_013/archive/2012/05/05/2484561.html 1. 协议 a. TCP/IP整体构架概述 TCP/IP协议并不完全 ...

  5. android下身份验证方式调用webservice

    在企业开发领域,webservice还是经常被用到的服务体系,因为他对安全事务支持都比较好. 有时候,我们就需要在android下调用后端的webservice服务,因为在内部网络环境下,所有需要ba ...

  6. Repeater上下排序按钮

    aspx代码 <table cellspacing="0" cellpadding="0" width="100%" align=&q ...

  7. Npoi Web 项目中(XSSFWorkbook) 导出出现无法访问已关闭的流的解决方法

    原本在CS项目中用的好好的在BS项目中既然提示我导出出现无法访问已关闭的流的解决方法 比较郁闷经过研究 终于解决了先将方法发出来 让遇到此问题的筒子们以作参考 //新建类 重写Npoi流方法 publ ...

  8. BZOJ 1724 切割木板

    合并果子. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm& ...

  9. BZOJ 4408 神秘数

    题解同各神犇的方法... #include<iostream> #include<cstdio> #include<cstring> #include<alg ...

  10. php 调试工具及学习PHP垃圾回收机制了解引用计数器的概念

    php代码工具:Xdebug  与分析工具 WinCacheGrind Xdebug之函数大全: string xdebug_call_class()返回当前被调用的函数或方法所属的类的类名 stri ...