[英]Copy constructor calls destructor c++
I have a test class of my to make my own string functions. 我有一个测试类来制作自己的字符串函数。 I have a problem with the copy destructor.
复制析构函数有问题。
I have 2 strings: s1 and s2. 我有2个字符串:s1和s2。 I call the function s3 = s1 + s2;
我称函数s3 = s1 + s2;
It first calls the operator+ function and when it's finished it calls the destructor. 它首先调用operator +函数,并在完成后调用析构函数。 Because of this the string object in the operator= function is empty.
因此,operator =函数中的字符串对象为空。 How can I fix this?
我怎样才能解决这个问题?
Destructor: 析构函数:
String::~String() {
if (this->str)
delete[] str;
str = NULL;
len = 0;
}
Copy Constructor: 复制构造函数:
String::String(const String& string) {
this->len = string.len;
if(string.str) {
this->str = new char[string.len+1];
strcpy(this->str,string.str);
} else {
this->str = 0;
}
}
operator=
: operator=
:
String & String::operator= (const String& string) {
if(this == & string)
return *this;
delete [] str;
this->len = string.len;
if(string.str) {
this->str = new char[this->len];
strcpy(this->str,string.str);
} else {
this->str = 0;
}
return *this;
}
operator+
: operator+
:
String& operator+(const String& string1 ,const String& string2)
{
String s;
s.len = string1.len + string2.len;
s.str = new char[string1.len + string2.len+1];
strcpy(s.str,string1.str);
strcat(s.str,string2.str);
return s;
}
operator+
should not return a local variable by reference. operator+
不应通过引用返回局部变量。
Change the return type of operator+
to String
. 将
operator+
的返回类型更改为String
。 Ie, make the signature: 即,进行签名:
String operator+( String const& lhs, String const& rhs )
You probably also want to write a "move constructor" for your String
class: String( String&& other )
if you are writing your code in C++11. 如果您正在用C ++ 11编写代码,则可能还想为
String
类编写一个“移动构造函数”: String( String&& other )
。
A simple move constructor: 一个简单的move构造函数:
String::String( String&& other ): len(other.len), str(other.str) {
other.len = 0;
other.str = nullptr;
}
This isn't required, because the copy in the return statement of your operator+
will probably be "elided" by your compiler under non-trivial optimization levels, but still good practice. 这不是必需的,因为在非平凡的优化级别下,您的
operator+
的return语句中的副本可能会被编译器“消除”,但仍是一种很好的做法。
It's calling the Destructor because String s
is going out of scope in your operator+ overload. 之所以调用Destructor是因为
String s
超出了您的operator +重载范围。 Your operator+ overload needs to be returning a copy instead of a reference. 您的operator +重载需要返回副本而不是引用。
Therefore you should change your operator+ to 因此,您应将operator +更改为
String operator+(const String& string1, const String& string2)
Yeah i got your problem 是的,我有你的问题
The thing is when you are returning a reference to a temp object from + operator function and then you are assigning this to other object in main So here = overloaded function gets called in to which you are passing a reference to an object that no longer exists 问题是,当您从+运算符函数返回对临时对象的引用,然后将其分配给main中的其他对象时,这里=调用了重载函数,您正在向该对象传递对不再存在的对象的引用
So either you can return a copy from + operator function 因此,您可以从+运算符函数返回副本
or 要么
you can pass a copy in the = overlaoded function 您可以在= overlaoded函数中传递副本
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.