浅谈C++对象的拷贝与赋值操作
来源:互联网 发布:h5免费制作软件 编辑:程序博客网 时间:2024/05/22 17:07
浅谈C++对象的拷贝与赋值操作
本文介绍的是C++对象的拷贝与赋值操作,希望对大家有帮助,一起来看。
AD:
我发现一些同事在用C++编写一个类时,知道什么时候需要实现拷贝构造函数和赋值操作,但不知道什么时候拷贝构造函数被调用,什么时候赋值操作被调用,甚至把二者混为一谈。
要弄明白这个问题,最简单的做法莫过于写个测试程序试一下。不过那样做也未必是好办法,实验的结果往往导致以偏概全的结论。不如好好想一下,弄清楚其中的原理,再去写程序去验证也不迟。
拷贝构造函数,顾名思义,等于拷贝 + 构造。它肩负着创建新对象的任务,同时还要负责把另外一个对象拷贝过来。比如下面的情况就调用拷贝构造函数:
- cstring str = strother;
赋值操作则只含有拷贝的意思,也就是说对象必须已经存在。比如下面的情况会调用赋值操作。
- str = strother;
不过有的对象是隐式的,由编译器产生的代码创建,比如函数以传值的方式传递一个对象时。由于看不见相关代码,所以不太容易明白。不过我们稍微思考一下,就会想到,既然是根据一个存在的对象拷贝生成新的对象,自然是调用拷贝构造函数了。
两者实现时有什么差别呢?我想有人会说,没有差别。呵,如果没有差别,那么只要实现其中一个就行了,何必要两者都实现呢?不绕圈子了,它们的差别是:
拷贝构造函数对同一个对象来说只会调用一次,而且是在对象构造时调用。此时对象本身还没有构造,无需要去释放自己的一些资源。而赋值操作可能会调用多次,你在拷贝之前要释放自己的一些资源,否则会造成资源泄露。
明白了这些道理之后,我们不防写个测试程序来验证一下我们的想法:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- class cstring
- {
- public:
- cstring();
- cstring(const char* pszbuffer);
- ~cstring();
- cstring(const cstring& other);
- const cstring& operator=(const cstring& other);
- private:
- char* m_pszbuffer;;
- };
- cstring::cstring()
- {
- printf("cstring::cstring\n");
- m_pszbuffer = null;
- return;
- }
- cstring::cstring(const char* pszbuffer)
- {
- printf("cstring::cstring(const char* pszbuffer)\n");
- m_pszbuffer = pszbuffer != null ? strdup(pszbuffer) : null;
- return;
- }
- cstring::~cstring()
- {
- printf("%s\n", __func__);
- if(m_pszbuffer != null)
- {
- free(m_pszbuffer);
- m_pszbuffer = null;
- }
- return;
- }
- cstring::cstring(const cstring& other)
- {
- if(this == &other)
- {
- return;
- }
- printf("cstring::cstring(const cstring& other)\n");
- m_pszbuffer = other.m_pszbuffer != null ? strdup(other.m_pszbuffer) : null;
- }
- const cstring& cstring::operator=(const cstring& other)
- {
- printf("const cstring& cstring::operator=(const cstring& other)\n");
- if(this == &other)
- {
- return *this;
- }
- if(m_pszbuffer != null)
- {
- free(m_pszbuffer);
- m_pszbuffer = null;
- }
- m_pszbuffer = other.m_pszbuffer != null ? strdup(other.m_pszbuffer) : null;
- return *this;
- }
- void test(cstring str)
- {
- cstring str1 = str;
- return;
- }
- int main(int argc, char* argv[])
- {
- cstring str;
- cstring str1 = "test";
- cstring str2 = str1;
- str1 = str;
- cstring str3 = str3;
- test(str);
- return 0;
- }
希望对你有帮助。
【编辑推荐】
- C++中static的用法总结
- C++内存管理的探讨
- C++的输出格式控制技巧分析
- C++多态技术的实现和反思
- C++中的指针用法汇集
- 浅谈C++对象的拷贝与赋值操作
- C++对象的拷贝与赋值操作
- C++对象的拷贝与赋值操作
- C++对象的拷贝与赋值操作
- C++对象的拷贝与赋值操作
- C++对象的拷贝与赋值操作
- C++对象的拷贝与赋值操作
- QObject的拷贝与赋值操作
- 【C++】拷贝构造函数与赋值操作符的本质性区别分析
- 如何禁止对象的拷贝和赋值操作
- 拷贝构造函数与赋值操作符的具体实现
- 拷贝构造函数与重载赋值操作符的区别
- 拷贝构造与赋值操作(string实例)【写的很棒!】
- ObjectiveC中的赋值,对象拷贝,浅拷贝与深拷贝
- ObjectiveC中的赋值,对象拷贝,浅拷贝与深拷贝
- JavaScript之对象拷贝与赋值
- Python中的对象赋值与拷贝
- 拷贝构造与赋值操作(string实例)
- Python包管理不同方式的区别
- android.content包-------AsyncQueryHandler
- loadrunner--相对初学者讲解比较不错的网站
- iOS开发个人独立博客收集
- ie6下的js调试工具companion.js
- 浅谈C++对象的拷贝与赋值操作
- maven学习过程记录--依赖1
- android跑马灯效果实现(一直滚动不受影响)
- GNU Make in Detail for Beginners
- 把自己用到过的转换记下来,留待以后备用
- NFC协议规范学习笔记汇总
- sql数据库安装到安装支持文件时崩溃
- tomcat扛不住啊
- LPCTSTR和CString的关系