c++17(5)-类,构造函数,复制构造函数,数组参数,返回数组(2)

#include <iostream>
#include "learn.h"
using namespace  std ;

int main(int argc, char **argv)
{

    RunMessage myMess1{1,MesType::Input,"hello1"};
	cout<<myMess1.getStr()<<endl;
    RunMessage myMess2(myMess1);
    myMess2.setStr("hello2");
	cout<<myMess2.getStr()<<endl;
 	cout<<myMess1.getStr()<<endl; 
    RunMessage myMess3(myMess1);
    myMess3=myMess2;
    myMess3.setStr("hello3");
 	cout<<myMess3.getStr()<<endl;   
 	cout<<myMess2.getStr()<<endl;
    cout<<myMess1.getStr()<<endl;
    return 0;
}
//learn.h
#include <iostream>
enum class MesType{Input,Output,Save,Load};
class RunMessage{
    public:
          RunMessage(int msId,MesType msType,char* msStr)
          {
               id=msId;
               type=msType;
               strcpy(messStr,msStr);
            }
          RunMessage& operator=(const RunMessage &otherMess)
          {
               std::cout<<"construct2..."<<std::endl;
                if(this == &otherMess) return *this;
               this->id=otherMess.id;
               this->type=otherMess.type;
               strcpy(this->messStr,otherMess.messStr);
                return *this;
          }  
            
          RunMessage(const RunMessage &otherMess)
          {
               std::cout<<"construct1..."<<std::endl;
               id=otherMess.id;
               type=otherMess.type;
               strcpy(messStr,otherMess.messStr);
            }            
         char* getStr(){
              return messStr;
         }
         void setStr(char* msStr){
              strcpy(messStr,msStr);            
         } 
    private:
        int id;
        MesType type;
        char messStr[20];
};
hello1
construct1...
hello2
hello1
construct1...
construct2...
hello3
hello2
hello1
Hit any key to continue...
#include <iostream>
#include "learn.h"
using namespace  std ;

int main(int argc, char **argv)
{

    RunMessage myMess1{1,MesType::Input,"hello1"};
	cout<<myMess1.getStr()<<endl;
    RunMessage myMess2(myMess1);
    myMess2.setStr("hello2");
	cout<<myMess2.getStr()<<endl;
 	cout<<myMess1.getStr()<<endl; 
    RunMessage myMess3(myMess1);
    myMess3=myMess2;
    myMess3.setStr("hello3");
 	cout<<myMess3.getStr()<<endl;   
 	cout<<myMess2.getStr()<<endl;
    cout<<myMess1.getStr()<<endl;
    return 0;
}
//learn.h
#include <iostream>
enum class MesType{Input,Output,Save,Load};
class RunMessage{
    public:
          RunMessage(int msId,MesType msType,char* msStr)
          {
               id=msId;
               type=msType;
               messStr=new char[20];
               strcpy(messStr,msStr);
            }
            ~RunMessage(){
                delete[] messStr;
            }
          RunMessage& operator=(const RunMessage &otherMess)
          {
               std::cout<<"construct2..."<<std::endl;
                if(this == &otherMess) return *this;
               this->id=otherMess.id;
               this->type=otherMess.type;
               strcpy(this->messStr,otherMess.messStr);
                return *this;
          }  
            
          RunMessage(const RunMessage &otherMess)
          {
               std::cout<<"construct1..."<<std::endl;
               id=otherMess.id;
               type=otherMess.type;
               messStr=new char[20];
               strcpy(messStr,otherMess.messStr);
            }            
         char* getStr(){
              return messStr;
         }
         void setStr(char* msStr){
              strcpy(messStr,msStr);            
         } 
    private:
        int id;
        MesType type;
        char *messStr;
};
hello1
construct1...
hello2
hello1
construct1...
construct2...
hello3
hello2
hello1

上面程序在复制构造函数中,生成了新的messStr

 messStr=new char[20];

如果没有这条语句,仍然是一个深复制,因为,不同实例的指针指向不同的字符数组(messStr在main函数中的栈中保存,而不是在堆中保存)

//learn.h
#include <iostream>
enum class MesType{Input,Output,Save,Load};
class RunMessage{
    public:
          RunMessage(int msId,MesType msType,char* msStr)
          {
               id=msId;
               type=msType;
               messStr=msStr;
            }

          RunMessage& operator=(const RunMessage &otherMess)
          {
               std::cout<<"construct2..."<<std::endl;
                if(this == &otherMess) return *this;
               this->id=otherMess.id;
               this->type=otherMess.type;
               this->messStr=otherMess.messStr;
                return *this;
          }  
            
          RunMessage(const RunMessage &otherMess)
          {
               std::cout<<"construct1..."<<std::endl;
               id=otherMess.id;
               type=otherMess.type;
               messStr=otherMess.messStr;
            }            
         char* getStr(){
              return messStr;
         }
         void setStr(char* msStr){
              messStr=msStr;            
         } 
    private:
        int id;
        MesType type;
        char *messStr;
};
#include <iostream>
#include "learn.h"
using namespace  std ;

int main(int argc, char **argv)
{

    RunMessage myMess1{1,MesType::Input,"hello1"};
	cout<<myMess1.getStr()<<endl;
    RunMessage myMess2(myMess2);
    myMess2.setStr("hello2");
	cout<<myMess2.getStr()<<endl;
 	cout<<myMess1.getStr()<<endl; 
    RunMessage myMess3(myMess1);
    myMess3=myMess2;
    myMess3.setStr("hello3");
 	cout<<myMess3.getStr()<<endl;   
 	cout<<myMess2.getStr()<<endl;
    cout<<myMess1.getStr()<<endl;
    return 0;
}
hello1
construct1...
hello2
hello1
construct1...
construct2...
hello3
hello2
hello1
Hit any key to continue...

那什么情况下会导致浅复制,即多个实例共享一
messStr的内容。下面实现了浅复制,只复制了第一层指针Mess *mess,对于第二层指针char *text没有实现复制。

#include <iostream>
#include "learn.h"
using namespace  std ;

int main(int argc, char **argv)
{

    RunMessage myMess1{1,MesType::Input,"hello1"};
	cout<<myMess1.getStr()<<endl;
    RunMessage myMess2(myMess1);
    myMess2.setStr("hello2");
    cout<<myMess2.getStr()<<endl;
    cout<<myMess1.getStr()<<endl; 
    RunMessage myMess3(myMess1);
    myMess3=myMess2;
    myMess3.setStr("hello3");
 	cout<<myMess3.getStr()<<endl;   
 	cout<<myMess2.getStr()<<endl;
    cout<<myMess1.getStr()<<endl;
    return 0;
}
//learn.h
#include <iostream>
#include<cstring>
enum class MesType{Input,Output,Save,Load};

struct Mess
{
    int length;
    char *text; 
    Mess(char *messText){
        length=strlen(messText);
        text=new char[length+1];
        strcpy(text,messText);
    }
};

class RunMessage{
    public:
          RunMessage(int msId,MesType msType,char* msStr)
          {
               id=msId;
               type=msType;
               mess=new Mess(msStr);
            }
     
         char* getStr(){
              return mess->text;
         }
         void setStr(char* msStr){
              mess->length=strlen(msStr);
              delete[] mess->text;
              mess->text=new char[mess->length+1];
              strcpy(mess->text,msStr);         
         } 
    private:
        int id;
        MesType type;
        Mess *mess;
};
hello1
hello2
hello2
hello3
hello3
hello3
//learn.h
#include <iostream>
#include<cstring>
enum class MesType{Input,Output,Save,Load};

struct Mess
{
    int length;
    char *text; 
    Mess(char *messText){
        length=strlen(messText);
        text=new char[length+1];
        strcpy(text,messText);
    }
};

class RunMessage{
    public:
          RunMessage(int msId,MesType msType,char* msStr)
          {
               id=msId;
               type=msType;
               mess=new Mess(msStr);
            }
           ~RunMessage(){
               if (mess->text!=NULL){
                    delete[] mess->text;
                  mess->text=NULL;                 
               }
           }
          RunMessage& operator=(const RunMessage &otherMess)
          {
               std::cout<<"construct2..."<<std::endl;
                if(this == &otherMess) return *this;
               this->id=otherMess.id;
               this->type=otherMess.type;
               this->mess=otherMess.mess;
                return *this;
          }  
            
          RunMessage(const RunMessage &otherMess)
          {
               std::cout<<"construct1..."<<std::endl;
               id=otherMess.id;
               type=otherMess.type;
               mess=otherMess.mess;
            }        
         char* getStr(){
              return mess->text;
         }
         void setStr(char* msStr){
              mess->length=strlen(msStr);
              delete[] mess->text;
              mess->text=new char[mess->length+1];
              strcpy(mess->text,msStr);         
         } 
    private:
        int id;
        MesType type;
        Mess *mess;
};
#include <iostream>
#include "learn.h"
using namespace  std ;

int main(int argc, char **argv)
{

    RunMessage myMess1{1,MesType::Input,"hello1"};
	cout<<myMess1.getStr()<<endl;
    RunMessage myMess2(myMess1);
    myMess2.setStr("hello2");
    cout<<myMess2.getStr()<<endl;
    cout<<myMess1.getStr()<<endl; 
    RunMessage myMess3(myMess1);
    myMess3=myMess2;
    myMess3.setStr("hello3");
 	cout<<myMess3.getStr()<<endl;   
 	cout<<myMess2.getStr()<<endl;
    cout<<myMess1.getStr()<<endl;
    return 0;
}
hello1
construct1...
hello2
hello2
construct1...
construct2...
hello3
hello3
hello3

完善复制构造函数,避免复制自身

//learn.h
#include <iostream>
#include<cstring>
enum class MesType{Input,Output,Save,Load};

struct Mess
{
    int length;
    char *text; 
    Mess(char *messText){
        length=strlen(messText);
        text=new char[length+1];
        strcpy(text,messText);
    }
};

class RunMessage{
    public:
          RunMessage(int msId,MesType msType,char* msStr)
          {
               id=msId;
               type=msType;
               mess=new Mess(msStr);
            }
           ~RunMessage(){
               if (mess->text!=NULL){
                    delete[] mess->text;
                  mess->text=NULL;                 
               }
           }
          RunMessage& operator=(const RunMessage &otherMess)
          {
               std::cout<<"construct2..."<<std::endl;
                if(this == &otherMess) return *this;
               this->id=otherMess.id;
               this->type=otherMess.type;
               this->mess=otherMess.mess;
                return *this;
          }  
            
          RunMessage(const RunMessage &otherMess)
          {
               std::cout<<"construct1..."<<std::endl;
               if(this == &otherMess) return;
               id=otherMess.id;
               type=otherMess.type;
               mess=otherMess.mess;
            }        
         char* getStr(){
              return mess->text;
         }
         void setStr(char* msStr){
              mess->length=strlen(msStr);
              delete[] mess->text;
              mess->text=new char[mess->length+1];
              strcpy(mess->text,msStr);         
         } 
    private:
        int id;
        MesType type;
        Mess *mess;
};
发布了385 篇原创文章 · 获赞 13 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/AI_LX/article/details/104166336