C++ 包含同一类的成员的类
我想构造一个包含自身的类,但我必须避免无休止的循环。例如,我开始上课C++ 包含同一类的成员的类,c++,C++,我想构造一个包含自身的类,但我必须避免无休止的循环。例如,我开始上课 #include <iostream> #include <vector> #include <memory> using namespace std; class box { protected: int id; vector<box*> intmega; int n = 10; public: box(box const& autre, int
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class box {
protected:
int id;
vector<box*> intmega;
int n = 10;
public:
box(box const& autre, int id) : n(autre.n), id(id) {
for (auto& element : autre.get_intmega()) {
intmega.push_back(new box(*element, id + 100));
}
cout << "A new object has seen light" << endl;
}
box(int id) : n(10), id(id) { cout << "Created Box" << endl; }
void ajoute2(box& autre) { intmega.push_back(new box(autre)); }
int size_() const { return intmega.size(); }
int number() const { return n; }
box* get() { return intmega[0]; }
vector<box*> get_intmega() const { return intmega; }
int getId() const { return id; }
~box() {
cout << this << endl;
for (auto element : this->intmega)
delete element;
}
};
void affichel(box const& autre) {
cout << "Box :" << autre.getId() << endl;
cout << "Box :" << &autre << endl;
}
void affiche(box& autre) {
for (auto* element : autre.get_intmega()) {
affichel(*element);
affiche(*element);
cout << endl;
}
}
int main() {
box box1(1);
box box2(2);
box box3(3);
box2.ajoute2(box3);
box1.ajoute2(box2);
box box4(box1, 4);
affiche(box1);
cout << "Box1 Address : " << &box1 << endl;
affiche(box4);
cout << "Box4 Address : " << &box4 << endl;
return 0;
}
#包括
#包括
#包括
使用名称空间std;
类框{
受保护的:
int-id;
矢量intmega;
int n=10;
公众:
盒子(盒子常数和autre,内部id):n(autre.n),id(id){
用于(自动元素:autre.get\u intmega()){
intmega.推回(新框(*元素,id+100));
}
我做了一些小小的修改,现在我的程序运行得很好
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class box
{
protected:
vector<box*> intmega;
int n = 10;
public:
box(box const &autre) : n(autre.n)
{
for (auto &element : autre.get_intmega())
{
intmega.push_back(new box(*element));
}
cout << "A new object has seen light" << endl;
cout<<this<<endl;
}
box()
{
cout << "Created Box" << endl;
cout<<this<<endl;
}
void ajoute2(box & autre){
intmega.push_back(new box(autre));
}
int size_() const
{
return intmega.size();
}
int number() const
{
return n;
}
vector<box *> get_intmega() const
{
return intmega;
}
~box()
{
cout<<this<<endl;
for(auto element: this->intmega){
delete element;
}
}
};
void affichel(box const &autre)
{
cout << "Box :" << &autre<< endl;
}
void affiche(box &autre)
{
for (auto *element : autre.get_intmega())
{
affichel(*element);
affiche(*element);
cout << endl;
}
}
int main()
{
box box1;
box box2;
box box3;
box box4;
box3.ajoute2(box4);
box2.ajoute2(box3);
box1.ajoute2(box2);
box box5(box1);
affiche(box1);
cout << "Box1 Address : " << &box1 << endl ;
affiche(box5);
cout << "Box5 Address : " << &box4 << endl ;
return 0;
}
#包括
#包括
#包括
使用名称空间std;
类框
{
受保护的:
矢量intmega;
int n=10;
公众:
箱(箱常数和autre):n(autre.n)
{
用于(自动元素:autre.get\u intmega())
{
intmega.push_back(新框(*元素));
}
cout Tip:std::shared_ptr
来封装它并为您处理销毁。如果您有多个引用指针,您将删除
不止一次,因此会崩溃。如果您真的想保留原始指针,您必须了解,因为这是我第一次遇到这样的事情,我从来没有使用过共享指针你解释得更详细一点。我根据每个对象都必须是唯一的这一事实进行编码。我知道我没有使用unique_ptr,但它可以工作。感谢你的即时响应subtle side note:在这里你不会遇到问题,但是成员总是按照类定义中定义的顺序构造的。这意味着id
will al方法在n
之前初始化,即使在:n(autre.n),id(id)
。这在这里不会有什么影响,但是如果id
的初始化依赖于n
的值,n
将不会被初始化,可能会导致很多令人费解的调试。您不应该需要框指针。您可以使用默认的移动和复制构造函数。替换vector intmega width vector intmega,删除您的destructor和avoid new Box这看起来可以解决问题(但尚未运行),但请注意上面Jimmy Loyola的评论。如果您要复制框
s并将副本存储在向量
中,您最好不要使用指针。向量
将以透明的方式为您完成所有工作。您无需付出任何努力。我相信从C++17开始,这是100%合法的,但通常可以作为expected在较旧的实现中也是如此。请不要在回答中询问后续问题。如果此解决方案有效,这就足够了。您应该添加一些关于您所更改内容的解释。如果您有新问题,请继续并发布。老实说,我使用了指针,因为实际上box是包含许多对象的子类另一门课,我想自己做一堆东西