zoukankan      html  css  js  c++  java
  • 深复制、浅复制

    #include <iostream>

    using namespace std;

    class CDemo {
    public:
      CDemo(int pa,char *cstr)
      {
        this->a = pa;
        this->str = new char[104];
        strcpy(this->str,cstr);
        //this->str = cstr;
      }

      int getA()
      {
        return this->a;
      }

      char* getCStr()
      {
        return this->str;
      }

      void setA(int a)
      {
        this->a = a;
      }

      void setStr(char *cstr)
      {
        this->str = new char[104];
        strcpy(this->str, cstr);
      }

      void setStr2(char *cstr)
      {
        this->str = cstr;
      }

      ~CDemo()
      {
        delete str;
      }
    private:
      int a;
      char *str;
    };

    int main()
    {
      CDemo A(10,"hello");
      cout <<"A:"<< A.getA()<<" "<<A.getCStr()<< endl;

      CDemo B = A;
      cout << "B:" << B.getA() << " " << B.getCStr() << endl;

      B.setA(30);
      B.setStr("word");//深复制
      //B.setStr2("word2");//浅复制

      cout << "B:" << B.getA() << " " << B.getCStr() << endl;
      cout << "A:" << A.getA() << " " << A.getCStr() << endl;


      system("pause");
      return 0;
    }

    -----------------------------------------------------------------------------------------------------

    A:10 hello
    B:10 hello
    B:10 word
    A:10 hello
    请按任意键继续. . .

  • 相关阅读:
    C#_简单实用的翻页
    C#注意事项及错误处理
    C# 委托和Lambda---基础
    C#_数据转换 实用方法
    C#图片处理---基础
    C#_使用SMTP发送邮件
    C#_生成HTML
    C#操作XML文档---基础
    百度地图 使用两条平行线表示路线
    Gradle version 2.10 is required. Current version is 2.8.
  • 原文地址:https://www.cnblogs.com/herd/p/10977573.html
Copyright © 2011-2022 走看看