Effective C++ Item 5 了解 C++ 默默编写并调用哪些函数
来源:互联网 发布:快递单统计软件 编辑:程序博客网 时间:2024/06/06 02:05
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
经验1:
如果你自己没声明,编译器会自动声明copy constructor,copy assignment,destructor,
如果你没有声明任何构造函数,编译器会自动声明default constructor
示例:
如果你写下
- class Empty{ };
将会等价于
- class Empty{
- public:
- Empty() {...} //default构造函数
- Empty(const Empty &rhs) {...} //copy 构造函数
- ~Empty() {...} //析构函数
- Empty &operator=(const Empty &rhs) { ... } //copy assignment 操作符
- }
只有当这些函数被调用时,编译器才会创建它们
- Empty e1; //default构造函数, 析构函数
- Empty e2(e1); //copy 构造函数
- e2 = e1; //copy assignment操作符
经验2:如果你打算在一个“内含reference成员”或“内含const成员”的class内支持赋值操作,你必须自己定义copy assignment操作符。因为C++并不允许让reference改
指向不同对象;const成员不可以被修改
示例:如果class“内含reference成员”或“内含const成员”,则编译器不会自动生成copy assignment操作符
- #include <iostream>
- using namespace std;
- template<class T>
- class NamedObject
- {
- public:
- NamedObject(std::string &name, const T &value): nameValue(name), objectValue(value){}
- private:
- std::string &nameValue; //reference
- const T objectValue; //const
- };
- int main(){
- string newDog("Persephone");
- string oldDog("Satch");
- NamedObject<int> p(newDog, 2);
- NamedObject<int> s(oldDog, 36);
- p = s;
- system("pause");
- }
输出:
(出错)error C2582: 'operator =' function is unavailable in 'NamedObject<T>'
0 0
- 【Effective C++】条款5:了解C++默默编写并调用哪些函数
- Effective C++——》条款5:了解C++默默编写并调用哪些函数 .
- Effective C++ Item 5 了解 C++ 默默编写并调用哪些函数
- Effective C++ Item 5 了解 C++ 默默编写并调用哪些函数
- Item 5:了解C++默默编写并调用哪些函数【effective C++读书笔记】
- 《Effective C++》学习笔记条款05了解C++默默编写并调用哪些函数
- Effective C++:条款05:了解C++默默编写并调用哪些函数
- [Effective C++]条款05 了解C++默默编写并调用哪些函数
- effective c++:了解c++默默编写并调用了哪些函数
- 读书笔记《Effective C++》条款05:了解C++默默编写并调用哪些函数
- Effective C++ Item 05-了解C++默默编写并调用哪些函数
- Item 05:了解C++默默编写并调用哪些函数
- 《Effective C++》读后总结(五)了解C++默默编写并调用哪些函数
- [effectiv c++]条款5:了解C++默默编写并调用哪些函数
- Effective C++学习5 条款05:了解C++默默编写并调用了哪些函数
- Effective c++学习笔记——条款5:了解C++默默编写并调用哪些函数
- Effective c++学习笔记——条款5:了解C++默默编写并调用哪些函数
- Effective C++ 条款5——了解C++默默编写并调用哪些函数
- nyoj290动物统计加强版
- 个人感悟2014,10,28
- 如何用fiddler2捕获移动设备上的http或者https请求
- 结构体拷贝
- MYSQL远程登录权限设置
- Effective C++ Item 5 了解 C++ 默默编写并调用哪些函数
- android快速开发框架
- Word Break II
- A+B Problem III
- Remove Element,Remove Duplicates frm Sorted Array I,II(List I,II),Valid Parentheses,ZigZag Conv
- HDU 4411 Arrest 最小费用流
- 易飞9.07衍生字段删除问题
- 简单http_server的C语言实现
- python语法,以缩进分块!