• C++左移运算符重载、浅拷贝、深拷贝


    左移运算符重载

    #include
    using namespace std;
    
    class Human{
        public:
        int x;
        int y;
        Human(int x1,int y1):x(x1),y(y1){};
    };
    
    //左移运算符重载,用到ostream
    ostream& operator<<(ostream& stream, const Human& other){//第一个参数固定,第二个参数才是传进来的参数
        cout<<
        "x:"<<other.x<<
        "   "  
        <<"y:"<<other.y<<endl;
        return stream;
    
    }
    
    int main(){
        Human h1(1,2);
        Human h2(3,4);        
        cout<<h1<<h2<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    运行结果:

    x:1   y:2
    x:3   y:4
    
    • 1
    • 2

    c++浅拷贝

    浅拷贝理解一

    #include 
    #include 
    using namespace std;
    
    int main(){
        
        int *a = new int(1);
        int *b = a;//此时b和a指向同一块地址,改b的值就会改a,这称为"浅拷贝"
        *b=2;
    
        cout <<"b:"<< *b << ",a:" << *a<<endl;
        cout<<"hello world"<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    运行结果:

    b:2,a:2
    hello world
    
    • 1
    • 2

    浅拷贝理解二

    #include 
    #include 
    #include 
    using namespace std;
    
    class String{
    private:
        char *m_str;
        unsigned int m_size;
    public:
        String(const char* str){
            m_size = strlen(str);
            m_str = new char[m_size+1];
            memcpy(m_str,
                   str,
                   m_size+1);//目的地,来源,大小
        }
        
        char& operator[](unsigned int index){
            return m_str[index];
        }
        friend ostream& operator<<(ostream& stream, const String& str);
    };
    
    ostream& operator<< (ostream& stream, const String& str){
        cout<<str.m_str<<endl;
        return stream;
    }
    
    int main(){
        String str1 = "hello__world";
        String str2 = str1;
    
        str2[0] = 'p';
        
        cout<<str1<<str2<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    运行结果:

    pello__world
    pello__world
    
    
    • 1
    • 2
    • 3

    解释:只复制了指针,但没有复制指针指向的结果

    浅拷贝总结

    浅拷贝就是:
    比如有a,b两个变量
    b和a指向了同一块地址,
    b发生更改,a也跟着变;
    a发生更改,b也跟着变.

    我们想要的并不是浅拷贝这样的结果 , 而是a,b互不干扰,
    即a变,b不变
    b变,a不变
    怎么做到呢?=======>c++深拷贝:使用拷贝构造函数

    c++深拷贝:使用拷贝构造函数

    深拷贝:有a,b两个变量,
    int a=1; int b;
    b要拷贝a的,就给b重新申请一个空间,
    让他们俩互不干扰

    /*关键代码:其实就是,
    先重新申请一段空间,再进行memcpy拷贝,
    就能完成深拷贝!!!*/
    /*拷贝构造函数*/
        String(const String& other)
            :m_size(other.m_size)
        {
            m_str = new char[m_size+1];
            memcpy(m_str,
                   other.m_str,
                   m_size+1);//来源,目的地,大小
        }
    /*拷贝构造函数*/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    完整代码:

    #include 
    #include 
    #include 
    using namespace std;
    
    class String{
    private:
        char *m_str;
        unsigned int m_size;
    public:
        String(const char* str){
            m_size = strlen(str);
            m_str = new char[m_size+1];
            memcpy(m_str,
                   str,
                   m_size+1);//来源,目的地,大小
        }
        ~String(){}
    
    /*拷贝构造函数*/
        String(const String& other)
            :m_size(other.m_size)
        {
            m_str = new char[m_size+1];
            memcpy(m_str,
                   other.m_str,
                   m_size+1);//来源,目的地,大小
        }
    /*拷贝构造函数*/
    
        char& operator[](unsigned int index){
            return m_str[index];
        }
        friend ostream& operator<<(ostream& stream, const String& str);
    };
    
    ostream& operator<< (ostream& stream, const String& str){
        cout<<str.m_str<<endl;
        return stream;
    }
    
    int main(){
        String str1 = "hello__world";
        String str2( str1 );
    
        str2[0] = 'p';
        
        cout<<str1<<str2<<endl;
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    运行结果:

    hello__world
    pello__world
    
    • 1
    • 2

    深拷贝总结

    从用法上来讲:

    浅拷贝就是:
    直接memcpy拷贝

    深拷贝就是:
    先重新申请一段空间,
    再进行memcpy拷贝


    从安全性上来讲:

    如果在析构函数中,delete类对象,
    浅拷贝
    就会报错 .
    因为,
    b浅拷贝了a,
    a和b共用一段存储空间,
    但会delete两次.
    在第一次delete完毕之后,已经没有类对象,需要delete,
    第二次delete就会释放NULL空间,
    自然会报错!!!

    如果在析构函数中,delete类对象,
    深拷贝
    不会报错 .
    因为,
    b深拷贝了a,
    a和b各自用各自的存储空间,
    当析构函数delete时,
    a和b会delete自己的空间,
    不会报错!!!

    课程地址

    课程地址

  • 相关阅读:
    Leetcode680:验证回文串 ||
    智慧环卫解决方案-最新全套文件
    深度学习——(9)神经网络参数详解
    SRT服务器SLS
    【前端学习 -Vue (6) 异步请求适合在哪个生命周期调用?】
    jQuery DOM基础操作
    【Rust日报】2023-10-22 Korvin - 一个 WASM 前端框架,比基线 vanillajs 实现快了 33%!...
    数据结构—直接插入排序(C语言实现)
    第四课 递归、分治
    腾讯云轻量应用服务器搭建跨境电商的方法步骤(非常详细)
  • 原文地址:https://blog.csdn.net/weixin_52668597/article/details/126709341