gpt4 book ai didi

c++ - 与 c++ 中原型(prototype)模式的示例解释混淆

转载 作者:行者123 更新时间:2023-11-28 01:55:07 25 4
gpt4 key购买 nike

有人发布了有关此模式的问题,但我没有想到问题的解决方案,因此发布了我的疑问...

在上面的示例中,如果所有实例都由 s_prototypes 变量初始化,并且下一次如果其中的任何对象将在 clone 方法中被新对象替换,那么现有对象会发生什么?它会造成内存泄漏吗??

据我从上面的例子中理解的两个陈述让我感到困惑

class Stooge {
public:
virtual Stooge* clone() = 0;
virtual void slap_stick() = 0;
};

class Factory {
public:
static Stooge* make_stooge( int choice );
private:
static Stooge* s_prototypes[4];
};

int main() {
vector roles;
int choice;

while (true) {
cout << "Larry(1) Moe(2) Curly(3) Go(0): ";
cin >> choice;
if (choice == 0)
break;
roles.push_back(
Factory::make_stooge( choice ) );
}

for (int i=0; i < roles.size(); ++i)
roles[i]->slap_stick();
for (int i=0; i < roles.size(); ++i)
delete roles[i];
}

class Larry : public Stooge {
public:
Stooge* clone() { return new Larry; }
void slap_stick() {
cout << "Larry: poke eyes\n"; }
};
class Moe : public Stooge {
public:
Stooge* clone() { return new Moe; }
void slap_stick() {
cout << "Moe: slap head\n"; }
};
class Curly : public Stooge {
public:
Stooge* clone() { return new Curly; }
void slap_stick() {
cout << "Curly: suffer abuse\n"; }
};

Stooge* Factory::s_prototypes[] = {
0, new Larry, new Moe, new Curly
};
Stooge* Factory::make_stooge( int choice ) {
return s_prototypes[choice]->clone();
}

Output
Larry(1) Moe(2) Curly(3) Go(0): 2
Larry(1) Moe(2) Curly(3) Go(0): 1
Larry(1) Moe(2) Curly(3) Go(0): 3
Larry(1) Moe(2) Curly(3) Go(0): 0
Moe: slap head
Larry: poke eyes
Curly: suffer abuse

但是当我们通过 make_stooge 方法调用 clone 方法时,它会返回新对象,如果返回新对象并将其替换为现有对象,那么现有对象将在此处造成内存泄漏(因为 new 运算符完全创建单独的对象而不返回现有对象一个在这里)...

所以这个例子让我感到困惑....

最佳答案

好吧,你是对的,s_prototypes 中的指针永远不会被删除,但至少这些对象无论如何都会在程序的整个运行时持续存在,所以除非有特定的操作需要在它们的析构函数中执行,那么如果它们在程序终止之前没有被删除,那么它就不是世界末日。这远不及有可能持续泄漏内存的代码糟糕,后者会导致程序使用的内存不断增加。

如果你愿意,你可以用静态实例代替它们,像这样:

#include <iostream>
#include <vector>
using namespace std;

class Stooge {
public:
virtual Stooge* clone() = 0;
virtual void slap_stick() = 0;
virtual ~Stooge() = default;
};

class Larry : public Stooge {
public:
Stooge* clone() { return new Larry; }
void slap_stick() {
cout << "Larry: poke eyes\n"; }
};
class Moe : public Stooge {
public:
Stooge* clone() { return new Moe; }
void slap_stick() {
cout << "Moe: slap head\n"; }
};
class Curly : public Stooge {
public:
Stooge* clone() { return new Curly; }
void slap_stick() {
cout << "Curly: suffer abuse\n"; }
};

class Factory {
public:
static Stooge* make_stooge( int choice );
private:
static Larry larry;
static Curly curly;
static Moe moe;
};

int main() {
vector<Stooge*> roles;
int choice;

while (true) {
cout << "Larry(1) Moe(2) Curly(3) Go(0): ";
cin >> choice;
if (choice == 0)
break;
roles.push_back(
Factory::make_stooge( choice ) );
}

for (int i=0; i < roles.size(); ++i)
roles[i]->slap_stick();
for (int i=0; i < roles.size(); ++i)
delete roles[i];
}

Stooge* Factory::make_stooge( int choice ) {
switch(choice) {
case 1:
return larry.clone();
case 2:
return curly.clone();
case 3:
return moe.clone();
default:
return nullptr;
}
}

关于c++ - 与 c++ 中原型(prototype)模式的示例解释混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41500457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com