复制构造函数问题
我试图理解这个复制构造函数问题。我的问题与程序退出后的析构函数有关。看来变量 char* title 没有被破坏,我认为这可能是错误的,谢谢
另一个问题是为什么当对象 x 等于 x2 时不调用赋值运算符。我正在使用 g++ 和代码块。
#include <iostream>
using namespace std;
class myClass
{
private:
int x;
int *p;
char* title;
public:
myClass(int tx, int* temp, char* newtitle)
{
x = tx;
cout << "int constructor called with value x = " << x << endl;
p = new int;
p = temp;
title = new char [strlen(newtitle)+1];
strcpy(title, newtitle);
}
myClass(const myClass& mc)
{
cout << "copy constructor called with value = " << mc.x << endl;
x = mc.x;
p = new int;
*p = *mc.p;
title = new char [strlen(mc.title)+1];
strcpy(title, mc.title);
}
myClass & operator = (const myClass & that)
{
cout << "assignment called" << endl;
if(this != &that)
{
x = that.x;
}
return *this;
}
~myClass()
{
if (p)
{
cout<<"deleting p"<<endl;
delete p;
}
else if(title)
{
cout<<"deleting title"<<endl;
delete[] title;
}
}
};
int main()
{
int pointee = 10;
int* p = &pointee;
myClass x(3,p,"hello");
//myClass y = myClass(3,p);
myClass x2 = x;
return 0;
}
I'm trying to understand this copy constructor problem. The question I have pertains to the destructor after the program exits. It seems the variable char* title is not destructed and I think this maybe wrong, thanks
Another question is why the assignment operator isn't called when the object x is equal to x2. I'm using g++ with codeblocks.
#include <iostream>
using namespace std;
class myClass
{
private:
int x;
int *p;
char* title;
public:
myClass(int tx, int* temp, char* newtitle)
{
x = tx;
cout << "int constructor called with value x = " << x << endl;
p = new int;
p = temp;
title = new char [strlen(newtitle)+1];
strcpy(title, newtitle);
}
myClass(const myClass& mc)
{
cout << "copy constructor called with value = " << mc.x << endl;
x = mc.x;
p = new int;
*p = *mc.p;
title = new char [strlen(mc.title)+1];
strcpy(title, mc.title);
}
myClass & operator = (const myClass & that)
{
cout << "assignment called" << endl;
if(this != &that)
{
x = that.x;
}
return *this;
}
~myClass()
{
if (p)
{
cout<<"deleting p"<<endl;
delete p;
}
else if(title)
{
cout<<"deleting title"<<endl;
delete[] title;
}
}
};
int main()
{
int pointee = 10;
int* p = &pointee;
myClass x(3,p,"hello");
//myClass y = myClass(3,p);
myClass x2 = x;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我在您的实际代码和一般方法中都看到了各种各样的问题。
首先,char*
title
不会被删除,因为您没有删除它。这可能是一个逻辑错误:您可能不需要
else
。你为什么把它放在那里?接下来,您将泄漏一个
int
,这里:刚刚
new
-ed 的int
被传入的值temp 覆盖
。稍后,您尝试在析构函数中删除该指针。但由于您要删除指向自动变量的指针,因此您将清理堆。这也是一个逻辑错误。解决方案:不要这样做:
p = temp;
然而,最终,您的方法在多个层面上都是有问题的。
int
?只需有一个类的int
成员即可。不要使用动态分配(例如new
和delete
),除非您确实必须这样做。char*
动态分配字符串。相反,请使用#include
中的std::string
#include
的内置std::auto_ptr
,但在其他库中还有许多其他选项,通常是更好的选择。这里比较流行的是 Boost 的智能指针。You have a variety of problems that I was able to see, both in your actual code and in your general approach.
First of all, the char*
title
isn't deleted because you don't delete it. This is probably a logical error:You probably don't need the
else
. Why did you put it there?Next, you are leaking an
int
, here:The
int
you justnew
-ed gets overwritten by the passed-in valuetemp
.Later, you try to delete this pointer in the destructor. But since you are deleting a pointer to an automatic variable, you will hose the heap. This is also a logical error. Solution: don't do this:
p = temp;
Ultimately however, your approach is questionable on multiple levels.
int
s in the first place? Just have anint
member of the class. Don't use dynamic allocation (egnew
anddelete
) unless you really have to.char*
s. Instead, usestd::string
from#include <string>
std::auto_ptr
from#include <memory.h>
but there are many other options, often better choices, in other libraries. A popular one here is Boost's smart pointers.如果
p
为非NULL
,则析构函数将删除p
;如果非NULL并且p
为NULL,它会删除title
。但是您的构造函数和复制构造函数始终都会创建
new p
和new title
。所以你需要一直检查并删除两者。Your destructor deletes
p
ifp
is non-NULL
; and it deletestitle
if it's non-NULL andp
is NULL.But both your constructor and your copy constructor create
new p
andnew title
all the time. So you need to check and delete both all the time.尝试
在析构函数中代替
and
代替
Try
instead of
and in the destructor
instead of