C++对象的拷贝与赋值操作
1,167 views| 2008-11-12| 李先静| Programming| | 3 条评论转载时请注明出处和作者联系方式
文章出处:http://www.limodev.cn/blog
作者联系方式:李先静 <xianjimli@gmail.com>
我发现一些同事在编写一个类时,知道什么时候需要实现拷贝构造函数和赋值操作,但不知道什么时候拷贝构造函数被调用,什么时候赋值操作被调用,甚至把二者混为一谈。
要弄明白这个问题,最简单的做法莫过于写个测试程序试一下。不过那样做也未必是好办法,实验的结果往往导致以偏概全的结论。不如好好想一下,弄清楚其中的原理,再去写程序去验证也不迟。
拷贝构造函数,顾名思义,等于拷贝 + 构造。它肩负着创建新对象的任务,同时还要负责把另外一个对象拷贝过来。比如下面的情况就调用拷贝构造函数:
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;
}
Share
Comments
Tags
Recent Posts
Most Viewed
- 系统程序员成长计划写作提纲 - 19,605 views
- Android IPC机制详解 - 6,277 views
- 系统程序员成长计划-走近专业程序员(上) - 6,253 views
- 系统程序员成长计划-写得又快又好的秘诀(一) - 5,391 views
- 系统程序员成长计划-背景知识 - 5,070 views
- i++循环与i–循环的执行效率 - 4,712 views
- 系统程序员成长计划-Write once, run anywhere(WORA)(上) - 4,701 views
- 系统程序员成长计划-走近专业程序员(下) - 4,254 views
- Linux下的调试工具 - 4,017 views
- Advanced Linux Sound Architecture (ALSA) 研究笔记 - 4,017 views
- 系统程序员成长计划-序 - 3,985 views
- 系统程序员成长计划-写得又快又好的秘诀(三) - 3,930 views
- 中国人与自由软件文化研究(搞笑版) - 3,735 views
- Android中的MessageQueue,Handler,Looper和Thread - 3,686 views
- 答复:我不会OOO,仍然可以XXX - 3,659 views
Categories
- Android (28)
- Broncho-A1-Hack (6)
- DirectFB (7)
- FTK(嵌入式GUI) (24)
- GTK+ (29)
- KVM hack notes (8)
- Linux Mobile (65)
- Management (5)
- Mozilla (9)
- Open Source (5)
- Programming (34)
- Tools (9)
- Uncategorized (23)
- Win32 (3)
- X Windows (31)
- 沉思录 (29)
- 系统程序员成长计划 (67)
Blogroll
gallery
Linux guru
推荐网站
Recent Comments
- Dig on 嵌入式GUI FTK设计与实现-事件源(FtkSource)
- 用心生活每一天 » GNU gprof: linux profiling tools 使用 on gcc profiling的工作原理
- JavaScript for: i++ vs i–-传播、沟通、分享-一直“有你” on i++循环与i–循环的执行效率
- Frankly Law on 嵌入式GUI FTK介绍(11)-交叉编译
- tracing on Linux下的调试工具
- ndljsn on FTK移植指南(初稿)
- tracing on 爬塘朗山
- tracing on GTK+(基于DirectFB)的字体处理
- Kely on 系统程序员成长计划写作提纲
- tracing on 爬塘朗山



November 12th, 2008
E
March 26th, 2009
恩,不过个人觉得对于新人,这个概念是那么的诱人,但是副作用又是那么的明显。
而你作为leader,能做的就是明确告诉他们可不可以用,以及为什么。
个人建议是完全不用,C++的class不像C的struct那么单纯,所以拷贝构造和赋值本身都是很容易造成隐含BUG的因素。
至于原因,参看symbian~那样一个完全C++的系统,从CBase开始就禁用了所有的拷贝构造及赋值~
qb_2008
March 28th, 2009
拷贝构造函数最好在遇到自身时把内部指针设为NULL,防止指针指错地址。
CString::CString(const CString& other)
{
if(this == &other)
{
printf(“CString::CString(const CString& other)\n”);
m_pszBuffer=NULL;
return;
}
printf(“CString::CString(const CString& other)\n”);
m_pszBuffer=other.m_pszBuffer!=NULL?strdup(other.m_pszBuffer) : NULL;
}
admin
March 29th, 2009
printf(”CString::CString(const CString& other)\n”);
m_pszBuffer=NULL;
这样处理存在问题吧。