C++(十)operator=

简介: 本文档介绍了 C++ 中赋值运算符 `operator=` 的重载方法,包括其概念、语法格式及特性,并通过实现一个 `mystring` 类展示了具体的代码示例。赋值运算符用于将一个已创建的对象赋值给另一个已创建的对象,需注意自赋值、内存泄漏和重析构等问题。文档中的 `mystring` 类实现了字符串对象的赋值、拼接及比较等功能。

operator=

赋值运算符重载

用一个己有对象,给另外一个己有对象赋值。两个对象均己创建结束后,发生的赋值行为。

语法格式

类名
{
    类名& operator=(const 类名& 源对象)
    拷贝体
}
class A
{
    A& operator=(const A& another)
    {
    //函数体
        return *this;
    }
};

特性

  1. 系统提供默认的赋值运算符重载,一经实现,不复存在。
  2. 系统提供的也是等位拷贝,也就浅拷贝,会造成内存泄漏,重析构。
  3. 要实现深深的赋值,必须自定义。 (1.自赋值 2.内存泄露 3.重析构)
  4. 返回引用,通常不能用 const 修饰。string a,b,c; (a= b) = c; (a+b) = c

实现string类

mystring.h

/**
 * Created by gopher on 24-7-28 下午6:45
 */
#ifndef CDEMO_MYSTRING_H
#define CDEMO_MYSTRING_H
#include <cstring>
#include <iostream>
using namespace std;

class mystring {
private:
    char * _str;
public:
    mystring();
    mystring(char * str);
    ~mystring();
    mystring(const mystring & another);
    mystring & operator = (const mystring & another);
    mystring operator + (const mystring & another);
    mystring & operator += (const mystring & another);
    bool operator > (const mystring & another);
    bool operator >= (const mystring & another);

    bool operator < (const mystring & another);
    bool operator <= (const mystring & another);

    bool operator == (const mystring & another);

    char at(int n);
    char& operator[](int idx);


    void dis();

};


#endif //CDEMO_MYSTRING_H

mystring.cpp


/**
 * Created by gopher on 24-7-28 下午6:45
 */
#include "mystring.h"


mystring::mystring(){
    _str = new char[1];
    _str[0] = '\0';
}
mystring::mystring(char * str){
    int len = strlen(str);
    _str = new char[len+1];
    strcpy(_str, str);
}
mystring::~mystring(){
    delete[] _str;
}
mystring::mystring(const mystring & another){
    _str = new char[strlen(another._str)+1];
    strcpy(_str, another._str);

}

mystring & mystring::operator = (const mystring & another){
    if (this == &another){
        return *this;
    }
    delete[] this->_str;
    int  len =strlen(another._str);
    _str = new char [len+1];
    strcpy(_str, another._str);
    return *this;
}

mystring mystring::operator + (const mystring & another){
    int  len = strlen(this->_str)+strlen(another._str);
    mystring ms;
    delete[] ms._str;
    ms._str= new char[len+1];
    strcpy(ms._str, this->_str);
    strcat(ms._str, another._str);
    return ms;
}


mystring& mystring::operator += (const mystring & another){
    int catlen=strlen(this->_str);
    int srclen=strlen(another._str);
    int len=catlen+srclen;
    this->_str= static_cast<char *>( realloc(this->_str, len+1) );
    memset(this->_str+catlen,0,srclen+1);
    strcat(this->_str, another._str);
    return *this;
}

bool mystring::operator > (const mystring & another){
    return strcmp(this->_str, another._str)>0;
}
bool mystring::operator >= (const mystring &another){
    return strcmp(this->_str, another._str)>=0;
}
bool mystring::operator < (const mystring &another){
    return strcmp(this->_str, another._str)<0;
}
bool mystring::operator <= (const mystring &another){
    return strcmp(this->_str, another._str)<=0;
}
bool mystring::operator == (const mystring &another){
    return strcmp(this->_str, another._str)==0;
}


char mystring::at(int n){
    if (n<0 || n>=strlen(_str)){
        throw "out of range";
    }else{
        return _str[n];
    }
}

char& mystring::operator [] (int idx){
    if (idx<0 || idx>=strlen(_str)){
        throw "out of range";
    }else{
        return _str[idx];
    }
}



void mystring::dis(){
    cout << _str << endl;
}
相关文章
|
11月前
|
安全 C++
【C++11保姆级教程】移动构造函数(move constructor)和移动赋值操作符(move assignment operator)
【C++11保姆级教程】移动构造函数(move constructor)和移动赋值操作符(move assignment operator)
567 0
|
4月前
|
程序员 编译器 C++
C++中的运算符重载(Operator Overloading)
C++中的运算符重载(Operator Overloading)
41 1
|
2月前
|
NoSQL 编译器 Redis
c++开发redis module问题之如果Redis加载了多个C++编写的模块,并且它们都重载了operator new,会有什么影响
c++开发redis module问题之如果Redis加载了多个C++编写的模块,并且它们都重载了operator new,会有什么影响
|
2月前
|
NoSQL Redis C++
c++开发redis module问题之避免多个C++模块之间因重载operator new而产生的冲突,如何解决
c++开发redis module问题之避免多个C++模块之间因重载operator new而产生的冲突,如何解决
|
4月前
|
存储 数据处理 C++
C/C++ 数据结构设计与应用(三):运算符重载的策略与实践 (Operator Overloading Strategies and Practices)
C/C++ 数据结构设计与应用(三):运算符重载的策略与实践 (Operator Overloading Strategies and Practices)
38 0
|
4月前
|
C++
C++ operator关键字的使用(重载运算符、仿函数、类型转换操作符)
C++ operator关键字的使用(重载运算符、仿函数、类型转换操作符)
57 0
|
编译器
C++11之用户自定义字面量(ClassType operator““_C(param...))
C++11之用户自定义字面量(ClassType operator““_C(param...))
79 0
|
Java 机器人 物联网
【C++初阶:内存管理】C/C++内存分布及管理方式 | new/delete实现原理及operator new和operator delete函数 下
【C++初阶:内存管理】C/C++内存分布及管理方式 | new/delete实现原理及operator new和operator delete函数
164 0
【C++初阶:内存管理】C/C++内存分布及管理方式 | new/delete实现原理及operator new和operator delete函数 下