码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-10-23 18:28:44      阅读:233      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!