[英]deep copying using copy constructor c++
I am trying to implement my own CString class. 我正在尝试实现自己的CString类。 I am facing some problem in shallow copying the pointer. 我在浅复制指针时遇到了一些问题。 Here is the 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]='\0';
}
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]='\0';
}
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]='\0';
}
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]='\0';
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]!='\0'){
i++;
}
return i;
}
char *m_value;
};
ostream& operator<<(ostream& os, myCString obj)
{
os<<obj.m_value;
return os;
}
and here is the main(): 这是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;
}
The issue is when shallow copy happens; 问题是何时进行浅拷贝? i know that writing 我知道写作
myCString *ptr2 = new myCString(ptr);
will fix the issue; 将解决问题; but i want to keep the main function intact and make some changes in the class. 但我想保持main函数完整并在类中进行一些更改。 Is there anyway i can do it? 反正我能做到吗? Thanks in advance. 提前致谢。
将您的myCString(char* strParam)
签名更改为
myCString(const char* strParam)
Your request is to make a deep copy of the objekt in the line myCString *ptr2 = ptr
, but this aim is not reachable by object's class programing, because this line only copies a pointer and does not involve the object's class. 您的请求是在myCString *ptr2 = ptr
行中制作对象的深层副本,但是该目标无法通过对象的类编程实现,因为该行仅复制一个指针,并且不涉及该对象的类。 If you like to call the copy constructor, You must write as You suggested: myCString *ptr2 = new myCString(ptr)
; 如果要调用复制构造函数,则必须按照建议的方式编写: myCString *ptr2 = new myCString(ptr)
; or You can write: 或者您可以写:
myCString mystr("Hi! This is myCString\n");
cout<<mystr;
myCString mystr2 =mystr;
cout<<mystr;
Another problem: calling function ostream& operator<<(ostream& os, myCString obj)
the copy operator myCString(const myCString& obj)
is called for parameter obj. 另一个问题:调用函数ostream& operator<<(ostream& os, myCString obj)
,将复制操作符myCString(const myCString& obj)
称为参数obj。 You should write 你应该写
ostream& operator<<(ostream& os, const myCString& obj)
to avoid this. 避免这种情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.