如何 return 对象列表而不破坏它们
how to return a list of objects without destroying them
如何修复函数 'func' 使其 return 对象不被破坏?
函数 'func' 必须将对象添加到列表中,然后 return 它们被销毁
Smoothy抽象class有一个纯虚的描述方法()。装饰者思慕雪
包含smoothy, description() 和getPret() 方法return 描述和价格
聚合平滑。
SmoothyCuFream 和 SmoothyCuUmbreluta classes 添加文本“cu crema”
在冰沙的描述中分别包含“cu umbreluta”。加了奶油的冰沙加价 2 欧元,带伞的加价 3 欧元。
BasicSmoothy class 是没有奶油和没有伞的冰沙,方法
description () returns smothy的名字
#include <iostream>
#include <vector>
using namespace std;
class Smoothy {
private:
int pret=0;
public:
virtual string descriere() = 0;
int getPret(){
return pret;
}
void setPret(int a) {
pret += a;
}
};
class BasicSmooty : public Smoothy {
private:
string nume;
public:
BasicSmooty(string n) :
nume { n } {}
string descriere() {
return nume;
}
};
class DecoratorSmoothy : public Smoothy {
private:
Smoothy* smooty;
public:
DecoratorSmoothy() = default;
DecoratorSmoothy(Smoothy* n) :
smooty{ n } {}
string descriere() {
return smooty->descriere();
}
int getPret() {
return smooty->getPret();
}
};
class SmootyCuFrisca : public DecoratorSmoothy {
private:
BasicSmooty bsc;
public:
SmootyCuFrisca(string desc) :
bsc{ desc } {}
string descriere() {
setPret(2);
return bsc.descriere() + " cu frisca ";
}
};
class SmootyCuUmbreluta : public DecoratorSmoothy{
private:
BasicSmooty bsc;
public:
SmootyCuUmbreluta(string desc) :
bsc{ desc } {}
string descriere() {
setPret(3);
return bsc.descriere() + " cu umbreluta ";
}
~SmootyCuUmbreluta() {
cout << "rip";
}
};
vector<Smoothy*> func(void)
{
std::vector<Smoothy*> l;
SmootyCuFrisca a1{ "smooty de kivi" };
SmootyCuUmbreluta a2{ "smooty de kivi" };
SmootyCuFrisca a3{ "smooty de capsuni" };
BasicSmooty a4{ "smooty simplu de kivi" };
l.push_back(&a1);
l.push_back(&a2);
l.push_back(&a3);
l.push_back(&a4);
return l;
}
int main() {
vector<Smoothy*> list;
// Here when i call func() objects are distroyed
list = func();
return 0;
}
在func
中,您将函数局部变量的地址存储在l
中。因此,当您从函数 return l
时,所有 Smoothy*
现在都指向无效内存。
要解决此问题,您可以为添加到 l
的每个指针分配内存,如下所示:
l.push_back(new Smoothy{a1}); // instead of l.push_back(&a1);
// etc. for a2, a3, ...
要真正摆脱这个问题,请考虑完全不使用指针。如果你的设计不需要它,你可以去掉指针,你会省去很多麻烦。
嗯,当一个方法returns时,当然所有local/automatic变量都被销毁了。在最新修订的 c++ 更改中,有 return && 修饰符,它调用移动语义,这意味着对于非 const local/automatic 对象,你 return,它窃取:克隆 returned 对象,创建一个新对象并复制所有基元和对象指针,然后将对象指针设置为 null,因此它们不能被析构函数 deleted/freed。 (注意C free of a null pointer什么都不做!)对于const,当然要深拷贝。
如何修复函数 'func' 使其 return 对象不被破坏?
函数 'func' 必须将对象添加到列表中,然后 return 它们被销毁
Smoothy抽象class有一个纯虚的描述方法()。装饰者思慕雪 包含smoothy, description() 和getPret() 方法return 描述和价格 聚合平滑。 SmoothyCuFream 和 SmoothyCuUmbreluta classes 添加文本“cu crema” 在冰沙的描述中分别包含“cu umbreluta”。加了奶油的冰沙加价 2 欧元,带伞的加价 3 欧元。 BasicSmoothy class 是没有奶油和没有伞的冰沙,方法 description () returns smothy的名字
#include <iostream>
#include <vector>
using namespace std;
class Smoothy {
private:
int pret=0;
public:
virtual string descriere() = 0;
int getPret(){
return pret;
}
void setPret(int a) {
pret += a;
}
};
class BasicSmooty : public Smoothy {
private:
string nume;
public:
BasicSmooty(string n) :
nume { n } {}
string descriere() {
return nume;
}
};
class DecoratorSmoothy : public Smoothy {
private:
Smoothy* smooty;
public:
DecoratorSmoothy() = default;
DecoratorSmoothy(Smoothy* n) :
smooty{ n } {}
string descriere() {
return smooty->descriere();
}
int getPret() {
return smooty->getPret();
}
};
class SmootyCuFrisca : public DecoratorSmoothy {
private:
BasicSmooty bsc;
public:
SmootyCuFrisca(string desc) :
bsc{ desc } {}
string descriere() {
setPret(2);
return bsc.descriere() + " cu frisca ";
}
};
class SmootyCuUmbreluta : public DecoratorSmoothy{
private:
BasicSmooty bsc;
public:
SmootyCuUmbreluta(string desc) :
bsc{ desc } {}
string descriere() {
setPret(3);
return bsc.descriere() + " cu umbreluta ";
}
~SmootyCuUmbreluta() {
cout << "rip";
}
};
vector<Smoothy*> func(void)
{
std::vector<Smoothy*> l;
SmootyCuFrisca a1{ "smooty de kivi" };
SmootyCuUmbreluta a2{ "smooty de kivi" };
SmootyCuFrisca a3{ "smooty de capsuni" };
BasicSmooty a4{ "smooty simplu de kivi" };
l.push_back(&a1);
l.push_back(&a2);
l.push_back(&a3);
l.push_back(&a4);
return l;
}
int main() {
vector<Smoothy*> list;
// Here when i call func() objects are distroyed
list = func();
return 0;
}
在func
中,您将函数局部变量的地址存储在l
中。因此,当您从函数 return l
时,所有 Smoothy*
现在都指向无效内存。
要解决此问题,您可以为添加到 l
的每个指针分配内存,如下所示:
l.push_back(new Smoothy{a1}); // instead of l.push_back(&a1);
// etc. for a2, a3, ...
要真正摆脱这个问题,请考虑完全不使用指针。如果你的设计不需要它,你可以去掉指针,你会省去很多麻烦。
嗯,当一个方法returns时,当然所有local/automatic变量都被销毁了。在最新修订的 c++ 更改中,有 return && 修饰符,它调用移动语义,这意味着对于非 const local/automatic 对象,你 return,它窃取:克隆 returned 对象,创建一个新对象并复制所有基元和对象指针,然后将对象指针设置为 null,因此它们不能被析构函数 deleted/freed。 (注意C free of a null pointer什么都不做!)对于const,当然要深拷贝。