首页 > 编程语言 > 详细

[C++] Deep copy ,Shallow copy, copy constructor,"="

时间:2015-10-23 18:25:53      阅读:224      评论:0      收藏:0      [点我收藏+]

Deep copy ,Shallow copy, copy constructor,"="

Dog.h

#pragma once
class Dog
{
public:
    char *name;
    Dog();
    Dog(const Dog &it);
    ~Dog();
    void operator =(const Dog &it);
};

 

Dog.cpp

 

#include "Dog.h"
#include<string.h>
#include<iostream>

using namespace std;

//Constructor
Dog::Dog()
{
    name = new char[20];
    memset(name,0,sizeof(name));
    strcpy(name,"xiaohuang");
    cout << "dog" << endl;
}
//Destructor
Dog::~Dog()
{
    delete []name;
    cout << "~dog" << endl;
}
// copy constructor
Dog::Dog(const Dog &it)
{
    name = new char[20];
    memset(name, 0, sizeof(name));
    strcpy(name, it.name);
    cout << "copy dog" << endl;
}
// overload = 
void Dog:: operator =(const Dog &it)
{
    strcpy(name, it.name);
    cout << " = " << endl;
}

 

main.cpp

#include"Dog.h""
using namespace std;


void test(){
    Dog d1;
    Dog d2 = d1;
    Dog d3;
    d3 = d1;
}
void main(){
    
    test();
    system("pause");
}

技术分享

 

[C++] Deep copy ,Shallow copy, copy constructor,"="

原文:http://www.cnblogs.com/tianhangzhang/p/4904934.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!