一个类有一个指针指向另一个类作为成员变量并将其推入向量

发布于 2025-01-16 07:12:40 字数 1944 浏览 1 评论 0原文

using namespace std;
class B {
public:
    B() :m_i(0), m_Name("") {};
    B(const int num, const string& name) :m_i(num), m_Name(name) {};
    void showInfo() {
        cout << this->m_i << ", " << this->m_Name << endl;
    }
    friend class A;
private:
    int m_i;
    string m_Name;
};

class A {
public:
    A(const int num, const string& name) :m_i(num), m_Name(name), ptr(nullptr) {};
    A(const A& orig) {
        m_i = orig.m_i;
        m_Name = orig.m_Name;
        ptr = new B;
        ptr = orig.ptr;
    }
    void showInfo() {
        cout << this->m_i << " " << this->m_Name << endl;
        if (ptr) {
            cout << ptr->m_i << " " << ptr->m_Name << endl;
        }
    }
    ~A() {
        delete ptr;
    }
    friend class C;
private:
    int m_i;
    string m_Name;
    B* ptr;
};
class C {   
public:
    void function() {
        A instanceA1(10, "Hello");
        A instanceA2(11, "Hello");
        A instanceA3(12, "Hello");
        {//another scope
            B* instanceB1 = new B(10, "Bye");
            instanceA1.ptr = instanceB1;
            B* instanceB2 = new B(11, "Bye");
            instanceA2.ptr = instanceB2;
            B* instanceB3 = new B(12, "Bye");
            instanceA3.ptr = instanceB3;
        }
        DB.push_back(instanceA1);
        DB.push_back(instanceA2);
        DB.push_back(instanceA3);

        DB[0].showInfo();
        DB[1].showInfo();
        DB[2].showInfo();
    };
private:
    vector<A> DB;
};

int main(void) {
    C console;
    console.function();
}

我必须构建 A 的复制构造函数,因为有一个指针作为成员变量,并且据我所知,push_back() 只执行对象的“浅复制”。 然而,虽然我想要的输出是 10 你好 10再见 11 你好 11再见 12 你好 12再见 它什么也不打印。 如果我在 A 的析构函数中删除 delete ptr; ,它会打印我想要的内容,但我很漂亮 肯定存在内存泄漏。 我在这里做错了什么?

using namespace std;
class B {
public:
    B() :m_i(0), m_Name("") {};
    B(const int num, const string& name) :m_i(num), m_Name(name) {};
    void showInfo() {
        cout << this->m_i << ", " << this->m_Name << endl;
    }
    friend class A;
private:
    int m_i;
    string m_Name;
};

class A {
public:
    A(const int num, const string& name) :m_i(num), m_Name(name), ptr(nullptr) {};
    A(const A& orig) {
        m_i = orig.m_i;
        m_Name = orig.m_Name;
        ptr = new B;
        ptr = orig.ptr;
    }
    void showInfo() {
        cout << this->m_i << " " << this->m_Name << endl;
        if (ptr) {
            cout << ptr->m_i << " " << ptr->m_Name << endl;
        }
    }
    ~A() {
        delete ptr;
    }
    friend class C;
private:
    int m_i;
    string m_Name;
    B* ptr;
};
class C {   
public:
    void function() {
        A instanceA1(10, "Hello");
        A instanceA2(11, "Hello");
        A instanceA3(12, "Hello");
        {//another scope
            B* instanceB1 = new B(10, "Bye");
            instanceA1.ptr = instanceB1;
            B* instanceB2 = new B(11, "Bye");
            instanceA2.ptr = instanceB2;
            B* instanceB3 = new B(12, "Bye");
            instanceA3.ptr = instanceB3;
        }
        DB.push_back(instanceA1);
        DB.push_back(instanceA2);
        DB.push_back(instanceA3);

        DB[0].showInfo();
        DB[1].showInfo();
        DB[2].showInfo();
    };
private:
    vector<A> DB;
};

int main(void) {
    C console;
    console.function();
}

I had to build a copy constructor of A since there is a pointer as a member variable and as far as I know push_back() only does a 'shallow copy' of an object.
However, although my desired output is
10 Hello
10 Bye
11 Hello
11 Bye
12 Hello
12 Bye
it prints nothing.
And if I delete delete ptr; in A's destructor, it prints what I wanted but I'm pretty
sure there is a memory leak.
What did I do wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

草莓酥 2025-01-23 07:12:40

这是你的复制构造函数:

A(const A& orig) {
    m_i = orig.m_i;
    m_Name = orig.m_Name;
    ptr = new B;
    ptr = orig.ptr;
}

你将 ptr 分配给一个新的 B,但然后你转身并丢弃它,使其指向原始的 B。我认为这不是你想要的。怎么样:

    ptr = new B(*orig.ptr);

这有帮助吗?

Here's your copy constructor:

A(const A& orig) {
    m_i = orig.m_i;
    m_Name = orig.m_Name;
    ptr = new B;
    ptr = orig.ptr;
}

You assign ptr to a new B but then you turn around and trash it so it points to the original B. I don't think that's what you want. How about this:

    ptr = new B(*orig.ptr);

Does that help?

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