Effective C++(12) 复制对象时要复制每一个成员
负责拷贝的两个操作:拷贝构造函数和重载赋值操作符。一句话总结,确保被拷贝对象的所有成员变量都做一份拷贝。
Demo
void logCall(const std::string& funcName); // log函数
class Date{ ... };
class Customer {
public:
....
Customer(const Customer& rhs);
Customer& operator=(const Customer& rhs);
private:
std::string name;
Date lastTransaction;
};
Customer::Customer(const Customer& rhs)
: name(rhs.name) // 忽略了lastTransaction成员变量
{
logCall("Customer copy constructor");
}
Customer& Customer::operato=(const Customer& rhs)
{
logCall("Customer copy assignment operator");
name = rhs.name; // 忽略了lastTransaction成员变量
return *this;
}
class PriorityCustomer: public Custoer {
public:
...
PriorityCustomer(const PriorityCustoer& rhs);
PriorityCustomer& operato=(const PriorityCustomer& rhs);
...
private:
int priority;
};
PriorityCustomer&
PriorityCustomer::PriorityCustomer (const PriorityCustomer& rhs)
: priority(rhs.priority)
{
logCall("PriorityCustoer copy constructor");
}
PriorityCustomer&
PriorityCustomer::operator=(const PriorityCustomer& rhs)
{
logCall("PriorityCustomer copy assignment operator");
priority = rhs.priority;
return *this;
}
PriorityCustomer的拷贝函数们拷贝了它声明的成员变量,却忽略了它所继承的Customer成员变量的拷贝,也没有指定实参传递给Customer的构造函数,因此,PriorityCustomer对象的Customer成分会被不带实参的Customer默认构造函数初始化。
PriorityCustomer&
PriorityCustomer::PriorityCustomer (const PriorityCustomer& rhs)
: Customer(rsh), priority(rhs.priority)
{
logCall("PriorityCustoer copy constructor");
} PriorityCustomer&
PriorityCustomer::operator=(const PriorityCustomer& rhs)
{
logCall("PriorityCustomer copy assignment operator");
Customer::operator=(rhs);
priority = rhs.priority;
return *this;
}
确保赋值所有local成员变量
调用所有base classes内的适当的拷贝函数
虽然重载赋值操作符和拷贝函数的代码长得很像,但是很难复用它们,因为重载赋值操作符时调用拷贝构造函数就像在构造一个已经存在的对象;同样地,令拷贝构造函数调用重载赋值操作符一样没有意义。
所以,复用这两个代码的方式就是写一个新的成员函数给两者调用。
拷贝构造函数和重载赋值操作符应该确保复制“对象内的所有成员变量”及“所有base class成分”;不要尝试拷贝构造函数调用重载赋值操作符或是反过来。
Effective C++(12) 复制对象时要复制每一个成员的更多相关文章
- Effective C++ 条款11,12 在operator= 中处理“自我赋值” || 复制对象时不要忘记每一个成分
1.潜在的自我赋值 a[i] = a[j]; *px = *py; 当两个对象来自同一个继承体系时,他们甚至不需要声明为相同类型就可能造成别名. 现在担心的问题是:假如指向同一个对象, ...
- EC笔记:第二部分:12、复制对象时勿忘其每一个成分
EC笔记:第二部分:12.复制对象时勿忘其每一个成分 1.场景 某些时候,我们不想使用编译器提供的默认拷贝函数(包括拷贝构造函数和赋值运算符),考虑以下类定义: 代码1: class Point{ p ...
- Effective C++ -----条款12: 复制对象时勿忘其每一个成分
Copying函数应该确保复制“对象内的所有成员变量”及“所有base class成分”. 不要尝试以某个copying函数实现另一个copying函数.应该将共同机能放进第三个函数中,并由两个cop ...
- Effective C++ 条款12:复制对象时勿忘其每一个成分
void logCall(const std::string& funcName); class Customer { public: ... Customer (const Customer ...
- Effective C++_笔记_条款12_复制对象时勿忘其每一个成分
(整理自Effctive C++,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 编译器会在必要时候为我们的classes创建copying函数, ...
- [Effective C++ --012]复制对象时勿忘其每一个成分
引言: 在深拷贝和浅拷贝的理解中,我们知道了“拷贝构造函数”一词,并且也了解了它的构成. A(const A& r); // 形式有多种,在这里只列出一个 因此,在值传递的应用场景里,我们可以 ...
- EC读书笔记系列之7:条款12 复制对象时勿忘其每一个成分
记住: ★copying函数应确保复制“对象内的所有成员变量”及“所有base class成分” ★不要尝试以某个copying函数实现另一个copying函数.应该将共同机能放进第三个函数中,并由两 ...
- 条款12:复制对象时勿忘其每一个成分(Copy all parts of an object)
NOTE: 1.Copying 函数应该确保复制“对象内的所有成员变量”及“所有base class成分”. 2.不要尝试以某个copying函数实现另一个copying函数.应该将共同机能放进第三个 ...
- C++复制对象时勿忘每一部分
现看这样一个程序: void logCall(const string& funcname) //标记记录 { cout <<funcname <<endl; } cl ...
随机推荐
- 华为-on练习--重复的字符过滤
称号: 请写一个字符串过滤程序,如果使用多个相同的字符出现在字符串中,字符首次出现在非过滤,. 比方字符串"abacacde"过滤结果为"abcde". 演示样 ...
- 使用requirejs实现模块化编程
> 序言 - -# 公司大了,业务多了,前端代码量也逐渐增大,我们渐渐的依赖js实现的交互越来越多,长期以来会导致我们的代码维护越来越困难,所以依赖的插件也越来越多..比如这样页面中有大量的js ...
- JavaScript语言核心之词法结构
编程语言的词法结构是一套基础性规则,用来描述如何使用这门语言来编写程序.作为语法的基础,它规定了诸如变量名是什么样的.怎么写注释,以及程序语句之间如何分割的等规则. 1.1字符集 JavaScript ...
- 编程算法基础-数字数码管-隐藏password
作业 数字数码管 个数码管) @@@@ 0 @ @ 1 2 @ @ 3 @@@@ ...
- interview(转)
http://ifeve.com/ali-think-12/ http://ifeve.com/think-in-ali-10/
- 2077 汉诺塔IV
Problem Description 还记得汉诺塔III吗?他的规则是这样的:不允许直接从最左(右)边移到最右(左)边(每次移动一定是移到中间杆或从中间移出),也不允许大盘放到小盘的上面.xhd在想 ...
- crawler_URL编码原理详解
经常写爬虫的童鞋,难免要处理含有中文的url,大部分时间,都知道url_encode,各个语言也都有支持,今天简单整理下原理,供大家科普 1.特征: 如果URL中含有非ASCII字符的话, 浏览器会对 ...
- hdu4324 Triangle LOVE (拓扑排序)
这是一道最简单的拓扑排序题,好久没看这个算法了! 有点生疏了! 后附上百度的资料; #include<stdio.h> #include<string.h> int in[50 ...
- python3使用smtplib发电子邮件
smtplib模块smtp简单邮件传输协议client实现.对于多功能性,有时,当你要发送带附件的邮件或图片,使用email.mime加载内容. 码,如以下: import smtplib impor ...
- 转载使用Flurl制作可复用的分页组件
使用Flurl制作可复用的分页组件 使用ASP.NET MVC查询时,一直使用MvcPaging组件,虽然需要自定义MvcPaging.Pager才能达到我想要的效果,但在没有较好的URL库时,还是这 ...