复制构造函数问题

发布于 2024-10-08 12:57:29 字数 1671 浏览 7 评论 0原文

我试图理解这个复制构造函数问题。我的问题与程序退出后的析构函数有关。看来变量 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把人绕傻吧 2024-10-15 12:57:29

我在您的实际代码和一般方法中都看到了各种各样的问题。

首先,char* title 不会被删除,因为您没有删除它。这可能是一个逻辑错误:

    if (p)
    {
        cout<<"deleting p"<<endl;
        delete p;
    }
    else if(title)
    {
        cout<<"deleting title"<<endl;
        delete[] title;
    }

您可能不需要 else。你为什么把它放在那里?

接下来,您将泄漏一个 int,这里:

    p = new int;
    p = temp;

刚刚 new-ed 的 int 被传入的值 temp 覆盖

稍后,您尝试在析构函数中删除该指针。但由于您要删除指向自动变量的指针,因此您将清理堆。这也是一个逻辑错误。解决方案:不要这样做:p = temp;

然而,最终,您的方法在多个层面上都是有问题的。

  1. 为什么首先要动态分配int?只需有一个类的 int 成员即可。不要使用动态分配(例如newdelete),除非您确实必须这样做。
  2. 不要使用 char* 动态分配字符串。相反,请使用 #include 中的 std::string
  3. 如果您确实需要动态分配,请不要使用原始指针。请改用智能指针。 C++ 附带了一个来自 #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:

    if (p)
    {
        cout<<"deleting p"<<endl;
        delete p;
    }
    else if(title)
    {
        cout<<"deleting title"<<endl;
        delete[] title;
    }

You probably don't need the else. Why did you put it there?

Next, you are leaking an int, here:

    p = new int;
    p = temp;

The int you just new-ed gets overwritten by the passed-in value temp.

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.

  1. Why do you dynamically allocate ints in the first place? Just have an int member of the class. Don't use dynamic allocation (eg new and delete) unless you really have to.
  2. Don't dynamically allocate strings using char*s. Instead, use std::string from #include <string>
  3. If you really need dynamic allocation, don't use raw pointers. Use smart pointers instead. C++ comes with one built-in, 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.
羁绊已千年 2024-10-15 12:57:29

如果 p 为非 NULL,则析构函数将删除 p;如果非NULL并且p为NULL,它会删除title

但是您的构造函数和复制构造函数始终都会创建 new pnew title 。所以你需要一直检查并删除两者。

Your destructor deletes p if p is non-NULL; and it deletes title if it's non-NULL and p is NULL.

But both your constructor and your copy constructor create new p and new title all the time. So you need to check and delete both all the time.

短叹 2024-10-15 12:57:29

尝试

*p = *temp;

在析构函数中代替

 p = temp;

and

if(title)

代替

else if(title)

Try

*p = *temp;

instead of

 p = temp;

and in the destructor

if(title)

instead of

else if(title)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文