酸菜猪蹄的程序人生
木下编程屯屯烫烫
   什么时候调用拷贝构造函数?什么时候调用赋值运算符?

    很多初学者容易搞不清楚。我来总结一下就是:

    当进行一个类的实例初始化的时候,也就是构造的时候,调用的是构造函数(如是用其他实例来初始化,则调用拷贝构造函数),非初始化的时候对这个实例进行赋值调用的是赋值运算符。

    示例如下:

 1 #include <iostream>
 2 using namespace std;
 3 /************************************************************************/
 4 /* 该例子用来说明copy constructor 和 赋值运算符的调用情况                      */
 5 /************************************************************************/
 6 class CTest
 7 {
 8 public:
 9     int m_muber;
10     CTest():m_muber(0)
11     {
12         cout << "CTest()" << endl;
13     }
14     CTest(const CTest& t)
15     {
16         cout << "CTest(const CTest& t)" << endl;
17         this->m_muber = t.m_muber;
18     }
19     CTest(const int& t)
20     {
21         cout << "CTest(const int& t)" << endl;
22         this->m_muber = t;
23     }
24     CTest& operator=(const CTest& t)
25     {
26         cout << "CTest& operator=(const CTest& t)" << endl;
27         this->m_muber = t.m_muber;
28         return *this;
29     }
30     CTest& operator=(const int& t)
31     {
32         cout << "CTest& operator=(const int& t)" << endl;
33         this->m_muber = t;
34         return *this;
35     }
36 
37 
38 };
39 int main()
40 {
41     cout << "*********CTest a****************" << endl;
42     CTest a;
43     cout << "*********CTest b(a)*************" << endl;
44     CTest b(a);
45     cout << "*********CTest c = a ***********" << endl;
46     CTest c = a;
47     cout << "*********CTest d = 5************" << endl;
48     CTest d = 5;
49 
50     cout << "*********b = a************" << endl;
51     b = a;
52     cout << "*********c = 5************" << endl;
53     c = 5;
54 
55     return 0;
56 }
57 

    例子中执行结果是:
*********CTest a****************
CTest()

*********CTest b(a)*************
CTest(const CTest& t)

*********CTest c = a ***********
CTest(const CTest& t)

*********CTest d = 5************
CTest(const int& t)

*********b = a************
******
CTest& operator=(const CTest& t)

*********c = 5************
******
CTest& operator=(const int& t)

 
posted on 2006-05-15 09:54 cooelaf 阅读(1124) 评论(4)  编辑 收藏 引用 所属分类: Pure C/C++
Comments
  • # re: 关于拷贝构造函数和赋值运算符
    <font color="#FF00FF" >Stone Jiang
    Posted @ 2006-05-15 10:31
    这个示例不错,区分出了拷贝构造函数和赋值函数.

    有一个问题及建议
    C++类设计中,最重要的四个函数则(Big Four Function)是
    构造函数,析构函数,拷贝构造函数和赋值函数,博主是否在本文中,没有正确的把构造函数和拷贝构造函数区分开来?
    CTest a;

    *********CTest a****************
    CTest()

    调用的应该叫"构造函数"而不是"拷贝构造函数"


    CTest b(a);

    CTest b = a 应是等价的,均调用
    "拷贝构造函数"

    建议:博主同时阐述C++类设计中的 Big Four Function.

      回复  更多评论   
  • # re: 关于拷贝构造函数和赋值运算符
    CoderDream
    Posted @ 2006-05-15 10:34
    41 cout << "*********CTest a****************" << endl;
    42 CTest a; // 调用默认构造函数
    43 cout << "*********CTest b(a)*************" << endl;
    44 CTest b(a); // 调用拷贝构造函数
    45 cout << "*********CTest c = a ***********" << endl;
    46 CTest c = a; // 调用拷贝构造函数
    47 cout << "*********CTest d = 5************" << endl;
    48 CTest d = 5; // 调用拷贝构造函数,参数为int 型,会进行类型转换:
    49
    50 cout << "*********b = a************" << endl;
    51 b = a; // 调用重载操纵符=
    52 cout << "*********c = 5************" << endl;
    53 c = 5; // 调用重载操纵符=,参数为int 型,会进行类型转换:
      回复  更多评论   
  • # re: 关于拷贝构造函数和赋值运算符
    cooelaf
    Posted @ 2006-05-15 10:41
    @&lt;font color=&quot;#FF00FF&quot; &gt;Stone Jiang


    嗯,我这里主要是想说明拷贝构造函数和赋值运算符的区别。所以没放在一般构造函数和拷贝构造函数上。希望不要误导了别人:)  回复  更多评论   
  • # re: 关于拷贝构造函数和赋值运算符
    lijun
    Posted @ 2006-05-15 14:11
    无意中找到你的博客了,本来是想找找:一个类在什么情况下 必须 写拷贝构造函数,在什么情况下可以不用写(即调用默认的拷贝构造函数).
    必须就是如果你没有写,程序在调用了拷贝构造函数后,就会出错!
    不用写就是,程序在调用了默认拷贝构造函数后,是正常的.
    其实这个问题很简单回答的.但是是怎样的情况使我想起到这里的呢!
    vector<Csocket> VECTOR;
    VECTOR m_SocketVector;
    ...
    接收ClientSocket函数:
    {
    Csocket sock;
    sock.m_hSock = accept(m_ServerSocket.m_hSocket,...
    m_SocketVector.push_back(sock);
    }
    这是必然调用拷贝构造函数,想想效果怎样!?
    当然上面的可以这样写:
    vector<Csocket*> VECTOR;
    VECTOR m_SocketVector;
    接收ClientSocket函数:
    {
    Csocket sock = new Csocket;
    sock->m_hSock = accept(m_ServerSocket.m_hSocket,...
    m_SocketVector.push_back(&sock);
    }
    那个更好呢?
    有一个假设,本程序实现的是短连接,会有不断的Socket连接,断开.
    当然断开时是会调用delete的. 也就说白了,用new程序会不断的申请堆空间,再不断的释放空间.这样作为一个服务器来说,时间长了会有堆的内存碎片,程序运行会变慢??但是用vector 系统会处理内存碎片的处理的(听说的) ??!
    考虑这个原因,将要怎样处理??
    但是用上一种方法的话,系统会不断的调用拷贝一个对象,再析构调那个临时对象.也是影响了效率!!??

      回复  更多评论   

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理