#include <iostream>
#include "learn.h"
using namespace std ;
int main(int argc, char **argv)
{
char mess[20]{"hello1"};
RunMessage *myMess=new RunMessage(1,MesType::Input,mess);
cout<<myMess->getStr()<<endl;
delete myMess;
return 0;
}
hello1
默认的复制构造函数
#include <iostream>
#include "learn.h"
using namespace std ;
int main(int argc, char **argv)
{
char mess[20]{"hello1"};
RunMessage myMess1{1,MesType::Input,mess};
cout<<myMess1.getStr()<<endl;
RunMessage myMess2(myMess1);
myMess2.setStr("hello2");
cout<<myMess2.getStr()<<endl;
return 0;
}
//learn.h
enum class MesType{Input,Output,Save,Load};
class RunMessage{
public:
RunMessage(int msId,MesType msType,char msStr[])
{
id=msId;
type=msType;
strcpy(messStr,msStr);
}
char* getStr(){
return messStr;
}
void setStr(char* msStr){
strcpy(messStr,msStr);
}
private:
int id;
MesType type;
char messStr[20];
};
hello1
hello2
自定义复制构造函数
//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(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];
};
#include <iostream>
#include "learn.h"
using namespace std ;
int main(int argc, char **argv)
{
char mess[20]{"hello1"};
RunMessage myMess1{1,MesType::Input,mess};
cout<<myMess1.getStr()<<endl;
RunMessage myMess2(myMess1);
myMess2.setStr("hello2");
cout<<myMess2.getStr()<<endl;
return 0;
}
hello1
construct1…
hello2
#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;
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
//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];
};
#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;
return 0;
}
hello1
construct1…
hello2
hello1
construct1…
construct2…