一个类有一个指针指向另一个类作为成员变量并将其推入向量
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 is10 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是你的复制构造函数:
你将 ptr 分配给一个新的 B,但然后你转身并丢弃它,使其指向原始的 B。我认为这不是你想要的。怎么样:
这有帮助吗?
Here's your copy constructor:
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:
Does that help?