使用复制构造函数 C++ 进行深度复制
deep copying using copy constructor c++
我正在尝试实现我自己的 CString class。我在浅复制指针时遇到了一些问题。
这是 class
#include<iostream>
#include<string.h>
using namespace std;
class myCString{
public:
myCString(){
m_value = NULL;
}
myCString(char* strParam){
int len = length(strParam);
m_value = new char[len+1];
for(int i=0;i<len; i++)
{
m_value[i] = strParam[i];
}
m_value[len]='[=10=]';
}
myCString(const myCString& obj)
{
int len = obj.length();
m_value = new char[len+1];
for(int i=0;i<len; i++)
{
m_value[i] = obj.m_value[i];
}
m_value[len]='[=10=]';
}
myCString(const myCString *obj)
{
int len = obj->length();
m_value = new char[len+1];
for(int i=0;i<len; i++)
{
m_value[i] = obj->m_value[i];
}
m_value[len]='[=10=]';
}
const int length() const
{
return length(m_value);
}
myCString operator=(myCString obj)
{
int i=0;
int length= obj.length();
m_value = new char[length + 1];
for(;i<obj.length(); i++)
{
m_value[i] = obj.m_value[i];
}
m_value[length]='[=10=]';
return m_value;
}
~myCString()
{
delete []m_value;
m_value = NULL;
}
friend ostream& operator<<(ostream& os, const myCString obj);
private:
const int length(char* strParam)const
{
int i=0;
while(strParam[i]!='[=10=]'){
i++;
}
return i;
}
char *m_value;
};
ostream& operator<<(ostream& os, myCString obj)
{
os<<obj.m_value;
return os;
}
这里是 main():
#include"myCString.h"
int main()
{
myCString *ptr = new myCString("Hi! This is myCString\n");
cout<<*ptr;
myCString *ptr2 = ptr;
delete ptr;
cout<<*ptr2;
delete ptr2;
return 0;
}
问题是浅拷贝发生时;我知道写作
myCString *ptr2 = new myCString(ptr);
将解决问题;但我想保持主要功能完整并在 class 中进行一些更改。
无论如何我能做到吗?
提前致谢。
改变你的
myCString(char* strParam)
签名给
myCString(const char* strParam)
您的要求是对行 myCString *ptr2 = ptr
中的对象进行深度复制,
但是对象的class编程是达不到这个目的的,因为这行只是复制了一个指针,并不涉及对象的class。
如果您想调用复制构造函数,则必须按照您的建议编写:
myCString *ptr2 = new myCString(ptr)
;
或者你可以写:
myCString mystr("Hi! This is myCString\n");
cout<<mystr;
myCString mystr2 =mystr;
cout<<mystr;
另一个问题:调用函数ostream& operator<<(ostream& os, myCString obj)
为参数 obj 调用复制运算符 myCString(const myCString& obj)
。你应该写
ostream& operator<<(ostream& os, const myCString& obj)
避免这种情况。
我正在尝试实现我自己的 CString class。我在浅复制指针时遇到了一些问题。 这是 class
#include<iostream>
#include<string.h>
using namespace std;
class myCString{
public:
myCString(){
m_value = NULL;
}
myCString(char* strParam){
int len = length(strParam);
m_value = new char[len+1];
for(int i=0;i<len; i++)
{
m_value[i] = strParam[i];
}
m_value[len]='[=10=]';
}
myCString(const myCString& obj)
{
int len = obj.length();
m_value = new char[len+1];
for(int i=0;i<len; i++)
{
m_value[i] = obj.m_value[i];
}
m_value[len]='[=10=]';
}
myCString(const myCString *obj)
{
int len = obj->length();
m_value = new char[len+1];
for(int i=0;i<len; i++)
{
m_value[i] = obj->m_value[i];
}
m_value[len]='[=10=]';
}
const int length() const
{
return length(m_value);
}
myCString operator=(myCString obj)
{
int i=0;
int length= obj.length();
m_value = new char[length + 1];
for(;i<obj.length(); i++)
{
m_value[i] = obj.m_value[i];
}
m_value[length]='[=10=]';
return m_value;
}
~myCString()
{
delete []m_value;
m_value = NULL;
}
friend ostream& operator<<(ostream& os, const myCString obj);
private:
const int length(char* strParam)const
{
int i=0;
while(strParam[i]!='[=10=]'){
i++;
}
return i;
}
char *m_value;
};
ostream& operator<<(ostream& os, myCString obj)
{
os<<obj.m_value;
return os;
}
这里是 main():
#include"myCString.h"
int main()
{
myCString *ptr = new myCString("Hi! This is myCString\n");
cout<<*ptr;
myCString *ptr2 = ptr;
delete ptr;
cout<<*ptr2;
delete ptr2;
return 0;
}
问题是浅拷贝发生时;我知道写作
myCString *ptr2 = new myCString(ptr);
将解决问题;但我想保持主要功能完整并在 class 中进行一些更改。 无论如何我能做到吗? 提前致谢。
改变你的
myCString(char* strParam)
签名给
myCString(const char* strParam)
您的要求是对行 myCString *ptr2 = ptr
中的对象进行深度复制,
但是对象的class编程是达不到这个目的的,因为这行只是复制了一个指针,并不涉及对象的class。
如果您想调用复制构造函数,则必须按照您的建议编写:
myCString *ptr2 = new myCString(ptr)
;
或者你可以写:
myCString mystr("Hi! This is myCString\n");
cout<<mystr;
myCString mystr2 =mystr;
cout<<mystr;
另一个问题:调用函数ostream& operator<<(ostream& os, myCString obj)
为参数 obj 调用复制运算符 myCString(const myCString& obj)
。你应该写
ostream& operator<<(ostream& os, const myCString& obj)
避免这种情况。