65,210
社区成员
发帖
与我相关
我的任务
分享#include "stdafx.h"
#include <iostream>
using namespace std;
class TestString
{
public:
TestString(){};
void Show();
~TestString();
TestString(char* strVlaue);
TestString& operator=(const TestString &testStr);
public:
char* m_str;
private:
};
TestString::~TestString()
{
delete[]m_str;
}
void TestString::Show()
{
cout<<"value="<<m_str<<endl;
}
TestString& TestString::operator =(const TestString &str)
{
if (this->m_str==str.m_str)
{
return *this;
}
this->m_str=new char(strlen(str.m_str)+1);
strcpy(this->m_str,str.m_str);
return *this;
}
TestString::TestString(char *strValue)
{
m_str=strValue;
}
void main()
{
TestString str1;
TestString str2("ok2");
TestString str3;
str1 = str1;
str2.Show();
getchar();
}#include <iostream>
using namespace std;
class TestString
{
public:
TestString()
{
m_str=NULL;
val=90.5;
}
TestString(const char* strVlaue);
void Show();
void setDouble(const double& para);
~TestString();
TestString& operator=(const TestString &testStr);
private:
char* m_str;
double val;
};
TestString::~TestString()
{
if(m_str!=NULL) // Here, you should be careful
delete[]m_str;
}
void TestString::Show()
{
cout<<"value="<<m_str<<endl;
}
void TestString::setDouble(const double& para)
{
val=para;
}
TestString& TestString::operator =(const TestString &str)
{
if(this == &str) //防止自赋值
{
return *this; //Then try this one, It's OK
}
cout<<"Enter copy"<<endl; // for debug
this->m_str=new char[strlen(str.m_str)+1]; ///here
strcpy(this->m_str,str.m_str);
return *this;
}
TestString::TestString(const char *strValue)
{
// m_str=strValue; // here different to: m_str=new char[strlen(strValue)+1];
// But I do think it should be this one, or it could cause shallow copy.
m_str=new char[strlen(strValue)+1];
strcpy(m_str,strValue);
}
int main()
{
TestString str1("ok2");
TestString str2("ok2");
str2.setDouble(65.7); //here make this two different.
//TestString str3;
str1 = str2; //But here fail to implement copy
str2.Show();
system("pause");
return 0;
}
#include <iostream>
using namespace std;
class TestString
{
public:
TestString()
{
m_str=NULL;
}
TestString(const char* strVlaue);
void Show();
~TestString();
TestString& operator=(const TestString &testStr);
private:
char* m_str;
};
TestString::~TestString()
{
if(m_str!=NULL)
delete[]m_str;
}
void TestString::Show()
{
cout<<"value="<<m_str<<endl;
}
TestString& TestString::operator =(const TestString &str)
{
if (strcmp(this->m_str,str.m_str)==0) //If I use this one, It could be the one you love
{
return *this;
}
cout<<"Enter copy"<<endl; // for debug
this->m_str=new char[strlen(str.m_str)+1]; ///here
strcpy(this->m_str,str.m_str);
return *this;
}
TestString::TestString(const char *strValue)
{
// m_str=strValue; // here different to: m_str=new char[strlen(strValue)+1];
// But I do think it should be this one, or it could cause shallow copy.
m_str=new char[strlen(strValue)+1];
strcpy(m_str,strValue);
}
int main()
{
TestString str1("ok2");
TestString str2("ok2");
//TestString str3;
str1 = str2; //Why the strings are the same, but it also copy?
str2.Show();
system("pause");
return 0;
}
#include <iostream>
using namespace std;
class TestString
{
public:
TestString()
{
m_str=NULL;
val=90.5;
}
TestString(const char* strVlaue);
void Show();
void setDouble(const double& para);
~TestString();
TestString& operator=(const TestString &testStr);
private:
char* m_str;
double val;
};
TestString::~TestString()
{
if(m_str!=NULL) // Here, you should be careful
delete[]m_str;
}
void TestString::Show()
{
cout<<"value="<<m_str<<endl;
}
void TestString::setDouble(const double& para)
{
val=para;
}
TestString& TestString::operator =(const TestString &str)
{
if(this == &str) //防止自赋值
{
return *this; //Then try this one, It's OK
}
cout<<"Enter copy"<<endl; // for debug
this->m_str=new char[strlen(str.m_str)+1]; ///here
strcpy(this->m_str,str.m_str);
return *this;
}
TestString::TestString(const char *strValue)
{
// m_str=strValue; // here different to: m_str=new char[strlen(strValue)+1];
// But I do think it should be this one, or it could cause shallow copy.
m_str=new char[strlen(strValue)+1];
strcpy(m_str,strValue);
}
int main()
{
TestString str1("ok2");
TestString str2("ok2");
str2.setDouble(65.7); //here make this two different.
//TestString str3;
str1 = str2; //But here fail to implement copy
str2.Show();
system("pause");
return 0;
}
#include <iostream>
using namespace std;
class TestString
{
public:
TestString()
{
m_str=NULL;
val=90.5;
}
TestString(const char* strVlaue);
void Show();
void setDouble(const double& para);
~TestString();
TestString& operator=(const TestString &testStr);
private:
char* m_str;
double val;
};
TestString::~TestString()
{
if(m_str!=NULL) // Here, you should be careful
delete[]m_str;
}
void TestString::Show()
{
cout<<"value="<<m_str<<endl;
}
void TestString::setDouble(const double& para)
{
val=para;
}
TestString& TestString::operator =(const TestString &str)
{
if (strcmp(this->m_str,str.m_str)==0) //If I use this one, It could be the one you love
{
return *this;
}
cout<<"Enter copy"<<endl; // for debug
this->m_str=new char[strlen(str.m_str)+1]; ///here
strcpy(this->m_str,str.m_str);
return *this;
}
TestString::TestString(const char *strValue)
{
// m_str=strValue; // here different to: m_str=new char[strlen(strValue)+1];
// But I do think it should be this one, or it could cause shallow copy.
m_str=new char[strlen(strValue)+1];
strcpy(m_str,strValue);
}
int main()
{
TestString str1("ok2");
TestString str2("ok2");
str2.setDouble(65.7); //here make this two different.
//TestString str3;
str1 = str2; //But here fail to implement copy
str2.Show();
system("pause");
return 0;
}
#include <iostream>
using namespace std;
class TestString
{
public:
TestString()
{
m_str=NULL;
}
TestString(const char* strVlaue);
void Show();
~TestString();
TestString& operator=(const TestString &testStr);
private:
char* m_str;
};
TestString::~TestString()
{
if(m_str!=NULL)
delete[]m_str;
}
void TestString::Show()
{
cout<<"value="<<m_str<<endl;
}
TestString& TestString::operator =(const TestString &str)
{
if (strcmp(this->m_str,str.m_str)==0) //If I use this one, It could be the one you love
{
return *this;
}
cout<<"Enter copy"<<endl; // for debug
this->m_str=new char[strlen(str.m_str)+1]; ///here
strcpy(this->m_str,str.m_str);
return *this;
}
TestString::TestString(const char *strValue)
{
// m_str=strValue; // here different to: m_str=new char[strlen(strValue)+1];
// But I do think it should be this one, or it could cause shallow copy.
m_str=new char[strlen(strValue)+1];
strcpy(m_str,strValue);
}
int main()
{
TestString str1("ok2");
TestString str2("ok2");
//TestString str3;
str1 = str2; //Why the strings are the same, but it also copy?
str2.Show();
system("pause");
return 0;
}
#include <iostream>
using namespace std;
class TestString
{
public:
TestString()
{
m_str=NULL;
}
TestString(const char* strVlaue);
void Show();
~TestString();
TestString& operator=(const TestString &testStr);
private:
char* m_str;
};
TestString::~TestString()
{
if(m_str!=NULL)
delete[]m_str;
}
void TestString::Show()
{
cout<<"value="<<m_str<<endl;
}
TestString& TestString::operator =(const TestString &str)
{
if (this->m_str==str.m_str)
{
return *this;
}
cout<<"Enter copy"<<endl; // for debug
this->m_str=new char[strlen(str.m_str)+1]; ///here
strcpy(this->m_str,str.m_str);
return *this;
}
TestString::TestString(const char *strValue)
{
// m_str=strValue; // here different to: m_str=new char[strlen(strValue)+1];
// But I do think it should be this one, or it could cause shallow copy.
m_str=new char[strlen(strValue)+1];
strcpy(m_str,strValue);
}
int main()
{
TestString str1("ok2");
TestString str2("ok2");
//TestString str3;
str1 = str2; //Why the strings are the same, but it also copy?
str2.Show();
system("pause");
return 0;
}