CodingLife99 2017-05-23 02:58 采纳率: 0%
浏览 923

这条语句调用的不是拷贝构造函数,到底是什么样的构造函数呢?

最近又碰到了拷贝构造函数的问题,又复习了网易云课堂《C++程序设计入门》这方面的内容,看到崔毅东老师的一段代码,心中有了一些疑问,想要问问大家:

主函数是这样写的:
Person person1(111, 1970, 5, 3);
Person person2(222, 2000, 11, 8);
person1 = Person(person2);//有疑问的是这一句,他到底调用的是什么样的构造函数呢?我知道肯定不是拷贝构造函数,因为拷贝构造函数的格式是这样的:Person person1(person2);且是在person1刚定义时进行的操作.

下面是本程序中Person类构造函数的声明:
 Person(int id, int year, int month, int day);//仅这一个构造函数
  • 写回答

2条回答 默认 最新

  • shen_wei 2017-05-23 06:52
    关注
    class Example
    {
    public:
        Example();   //默认函数
        ~Example();  //析构函数
        Example(int n); //重构函数
        Example(const Example&); //拷贝构造函数
        Example& operator = (const Example&); //赋值符重载
    
    public:
        char *pBuffer;
        int nSize;
    };
    
    Example::Example()
    {
        pBuffer = NULL; 
        nSize = 0;
    }
    
    Example::Example(const Example& RightSides)
    {
        pBuffer=NULL;
        *this = RightSides; 
    }
    
    Example::~Example()
    {
        if (pBuffer != NULL)
        {
            delete pBuffer;
        }
    }
    
    Example& Example::operator = (const Example& RightSides)
    {
        nSize = RightSides.nSize;
        char *temp = new char[nSize];
        memcpy(temp, RightSides.pBuffer, nSize*sizeof(char));
        delete []pBuffer; 
        pBuffer = temp;
        return *this;
    }
    
    Example::Example(int n)
    {
         pBuffer = new char[n]; 
         nSize = n;
    }
    
    
    int main()
    {
        Example A(40),B(60);
    
        A = B;
    
        return 0;
    } 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题