首页 > 编程语言 > 详细

C++拷贝构造&操作符重载

时间:2017-03-05 18:16:32      阅读:258      评论:0      收藏:0      [点我收藏+]

头文件 DString.h如下

#ifndef __DSTRING_H
#define __DSTRING_H
#endif

#include <stddef.h>

class DString
{
    public:
        DString(const char * c = NULL);  // default constructor
        DString(const DString &str);  // copy constructor - implement DEEP copy

        ~DString();

        /*
        必须使用成员函数重载的运算符 = ( ) [ ]  ->
        必须使用友元函数重载的运算符 << >>
        P.S. 一目运算符,建议使用成员函数;二目运算符,建议只用友元函数
        */
        DString& operator = (const DString &str);
friend DString
operator + (const DString &s1, const DString &s2);
char* dcopy(char * dst, const char * src); private: char * m_data; };

实现如下DString.cpp

技术分享
#include <string.h>

#include "DString.h"

DString::DString(const char * c)
{
    if(NULL == c)
    {
        m_data = new char(\0);
    }
    else
    {
        m_data=new char[strlen(c) + 1];
        dcopy(m_data, c);
    }
}

DString::DString(const DString &str)
{
    if(NULL== str.m_data)
    {
        m_data=NULL;
    }
    else
    {
        m_data=new char[strlen(str.m_data) + 1];
        dcopy(m_data, str.m_data);
    }
}

DString::~DString()
{
    delete [] m_data;
}

DString& DString::operator = (const DString &str)
{
    if(NULL==str.m_data)
    {
        m_data=NULL;
        return *this;
    }
    else
    {
        delete [] m_data;
        m_data=new char[strlen(str.m_data)+1];
        dcopy(m_data,str.m_data);
        return *this;
    }
}

// no ::  because friend function doesnt belong to the class
DString operator + (const DString &s1, const DString &s2)
{
    if(NULL == s1.m_data)
    {
        return DString(s2);
    }

    if(NULL == s2.m_data)
    {
        return DString(s1);
    }

    char * temp = new char[strlen(s1.m_data) + strlen(s2.m_data) + 1];
    strcpy(temp, s1.m_data);
    strcpy(temp+strlen(s1.m_data), s2.m_data);

    return DString(temp);
}

char* DString::dcopy(char * dst, const char * src)
{
    char * tmp = dst;

    while(*src != \0)
    {
        *tmp++ = *src++;
    }

    *tmp = 0;

    return dst;
}
DString.cpp

调用入口main.cpp

#include "DString.h"

int main(int argc, char* argv[])
{
    DString s1("abcde");    //call default cons
    DString s2 = "ABC";        //call default cons

    DString s3 = s2;            //call copy cons
    DString s4(s3);            //call copy cons

    s1 = s4;                        //call operator =

    s4 = s1 + s2 + "ding"; //call default cons then + then + then =
    return 0;
}

 

一些知识点(以在代码中出现的顺序排列)

  1. 对于一个空的类,默认会有6个成员函数:构造/析构/拷贝构造/赋值/const取址/非const取址
  2. 在函数声明时使用了默认参数,则在实现时就不能再次指定默认参数
  3. 对于基本类型(没有析构函数)释放内存时delete同delete[] (原则是与NEW NEW[]配对使用)
  4. 显示声明拷贝构造函数以实现深拷贝(否则自动生成浅拷贝版本),调用场景为:根据已有对象初始化新对象
  5. 显示声明赋值函数,若已实现深拷贝
  6. 重载二元操作符时,应使用友元函数(若使用成员函数实现则只需传递一个参数,另一个参数是自己)
    • 友元函数/类,是通往私有成员的后门。其设计宗旨是,一种外部函数/类也可以访问类的私有成员
    • 友元函数不属于类,因此实现时,不需要也不能加::
    • 友元函数不能使用this指针,因此使用时通常需要把类的对象作为参数传入
    • 友元函数的典型使用场景是:一个函数需要访问多个类
  7. 重载+返回的是临时对象,因此不能函数返回值类型不能是引用

C++拷贝构造&操作符重载

原文:http://www.cnblogs.com/deanding/p/6506050.html

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