13.1.1 合成的复制构造函数 memberwise initialize问题 C++ Primer
来源:互联网 发布:变色龙制作淘宝客app 编辑:程序博客网 时间:2024/04/30 10:20
http://social.msdn.microsoft.com/Forums/zh/vcgeneral/thread/791dc8a1-ada1-4490-85fe-6912d4fe3734
Hi All,
I got a problem here. Somebody help me please.
To my understanding, there are two facts:
1. shallow copy = bitwise copy and deep copy = memberwise copy
2. The compiler will give us a default copy constructor if needed when we do not provide it. And this constructor is memberwise copy.
So i'm confused now:
If the default copy constructor is memberwise copy(which means deep copy), why should we define our own copy constructor? Because the default one is enough. But as we encounter the following scenario, we do need to provide our own copy constructor:
Class Test{int a;char *b;};
So could the <<C++ Primer>> wrong? Because it says the default copy constructor of C++ will do memberwise copy, but in fact, it does bitwise copy.
Or i missed something?
Please help me buddies.
Thanks.
Well, I think is a hybrid as I understand this topic: The compiler-provider copy constructor will perform a shallow copy on data types that don't provide a copy constructor themselves. Example: The compiler should provide a shallow copy of the pointer inside the following class, but it should provide a memberwise copy of the STL string:
class Test{ char *myShallowString; //The pointer will be cloned (shallow copy) std::string myMemberString; //The std::string's copy constructor will be used (memberwise copy)};So if you really analyze it, C++ truly does memberwise by default: It will call the copy constructor of a char pointer, which is one compiler-provided.
Correct. If all members of the class already provide an appropriate copy constructor, the class will behave properly, meaning in my head that C++ truly provides memberwise copy construction by default.
- 13.1.1 合成的复制构造函数 memberwise initialize问题 C++ Primer
- c++ 合成的复制构造函数
- c++primer 3/12----复制构造函数
- 编译器合成复制构造函数
- C++编译器合成构造函数的问题
- 验证:合成的复制构造函数是位拷贝的
- C++ Primer复制控制 13.1复制构造函数
- c++ primer 13.1.4节练习(合成的拷贝构造函数与自定义拷贝函数区别)
- 三法则,合成赋值操作符,合成复制构造函数
- 拷贝构造函数:bitwise vs. memberwise
- 合成的默认构造函数
- C++13.1.2合成复制构造函数----定义复制对象时会发生什么
- C++: 复制构造函数
- 复制构造函数问题
- C++:复制构造函数的应用
- Primer Plus C++ 复制构造函数
- 一点关于复制构造函数的问题
- 复制构造函数应该注意的问题
- 基于jxl的导出Excel通用方法
- Android下Dialog及Activity屏蔽Home键详解
- MySQL参数调优
- 七武器——.NET工程师必杀技
- iphone ios navagationbar navigationItem 顶部如何实现下拉框 dropdownlist
- 13.1.1 合成的复制构造函数 memberwise initialize问题 C++ Primer
- android学习笔记11:按钮
- Mysql 安装
- 《DB 查询分析器》使用技巧之(六)
- java设计模式之静态代理
- C# 一些自己需要知道的事情
- JDK安装
- Webkit内核探究【1】——Webkit简介
- 操作xml