- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想创建一个管理器类来管理所有已创建的一种类型的对象。自然这个类也应该创建这些对象。因此,不允许客户端自己创建对象,而必须始终使用管理器类来创建对象。此外,允许客户定义他自己的类,这些类将由一个管理器管理。
template<class Type>
class Manager{
//...
Type* createInstance(){
Type* ptr = new Type();
//do sommething
return ptr;
}
};
问题是:如何将实例的创建仅限于管理器类?
一种可能是将构造函数声明为私有(private)的,将 Manager 类声明为友元类:
class A{
friend class Manager<A>;
private:
A(){}
~A(){}
};
由于允许客户定义自己的类,他可以将构造函数声明为公共(public)的,而管理器仍然可以毫无问题地工作。但是客户端将能够在管理器类不在场的情况下创建这些类的实例,从而导致框架的不良行为/资源管理。
有没有办法克服这个问题?
最佳答案
这种类型的经理通常是一种糟糕的模式。以下代码示例说明了原因。
这与说所有析构函数都应该是虚函数的问题是同一个问题。我使用管理器创建一个 B,它派生自 A,将对象分配给 A* 指针。如果它被这样销毁,它会通过 Manager::destroy(),这可能不会很好地结束,因为它是通过 Manager::make() 创建的
#include <iostream>
using namespace std;
template<class Type>
class Manager
{
public:
Manager(char *type)
{
mytype = type;
}
Type *make()
{
Type *ptr = new Type();
cout << "in Manager<" << mytype << ">::make() ptr->mytype is " << ptr->mytype << endl;
return ptr;
}
void destroy(Type *ptr)
{
cout << "in Manager<" << mytype << ">::destroy() ptr->mytype is " << ptr->mytype << endl;
delete ptr;
}
private:
char *mytype;
};
class A
{
friend class Manager<A>;
protected:
A()
{
mytype = "A";
cout << "in A()" << endl;
}
virtual ~A()
{
cout << "in ~A() mytype is " << mytype << endl;
}
char *mytype;
};
class B : public A
{
friend class Manager<B>;
protected:
B()
{
mytype = "B";
cout << "in B()" << endl;
}
virtual ~B()
{
cout << "in ~B() mytype is " << mytype << endl;
}
};
int main()
{
Manager<A> ma("A");
Manager<B> mb("B");
B *b = mb.make();
A *a = b;
ma.destroy(a); // This line calls Manager<A>::destroy. It should call Manager<B>::destroy.
return 0;
}
产生以下输出:
in A()
in B()
in Manager<B>::make() ptr->mytype is B
in Manager<A>::destroy() ptr->mytype is B -- Oops - wrong destroy.
in ~B() mytype is B
in ~A() mytype is B
这反过来意味着您不能使用继承来发挥它的全部功能,这首先违背了使用 OO 语言的目的。
根据您认为需要经理的原因,可能有更好的解决方案。
如果它用于内存管理(不太可能考虑到您的原始代码片段),覆盖 new 和 delete 值得一看。
如果它是为了跟踪所有实例以进行处理(例如,游戏中的对象在每个游戏节拍都会更新),那么虽然这有点心理变速,但更好的解决方案是将管理器集成到类本身中,作为一组静态成员函数和变量。这种类型的管理器几乎总是单例,因此使它们成为静态成员/函数可以使您在语义上达到相同的位置。
所以你可能有:
static set<A *> s_collection;
static void Heartbeat() // in class A
{
// lock s_collection here
for (auto it = s_collection.begin, end = s_collection.end() it != end; ++it)
{
// process *it
}
// unlock s_collection here
}
然后在 A::A() 期间将其插入到 s_collection 中,同样在 A::~A() 期间将其删除。
如果您是多线程的,请小心使用合适的同步原语,因为大多数 STL 容器本身并不是线程安全的。
关于c++ - 仅在 Manager 类中创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25834300/
I have created a hybrid activation and then setup an ssm agent on my on-premise windows system.我创
我对 python/django 编程很陌生,因为我没有编程背景。我正在在线上课,我只想确切地知道 manage.py 文件的作用。我试过用谷歌搜索它,但除了在 django-admin.py 周围放
我的 DependancyInject 存在结构问题。 情况 我正在为基于体素的游戏创建服务器;它是完全调制的,但相关模块有以下3个。 NetworkModule(发送和接收数据包)WorldModu
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 5年前关闭。 Improve thi
上 Docker正在编写的网站: The MANAGER STATUS column shows node participation in the Raft consensus: No value
我正在尝试使用发布管理作为构建版本的工具,但我很难理解码件、工具和操作之间的真正区别。有人可以分解这三个概念之间的差异以及它们如何相互配合吗? 最佳答案 由于它适用于基于代理的版本: 工具旨在提供自定
当尝试在远程环境中在 pycharm 中执行“run manage.py Task...”时,出现以下错误: ssh://vagrant@127.0.0.1:2222/home/vagrant/.vi
在过去的 48 小时里,我一直在努力解决这个问题,这让我发疯了。 我的 SDK Manager.exe 闪烁一个 cmd 屏幕并在不到一秒内关闭。 经过多方搜索,我终于在调整android.bat并以
我在 this tutorial 之后创建了以下自定义管理命令. from django.core.management.base import BaseCommand, CommandError f
我在一家拥有 2,500 多名员工和同样多的 Android 智能手机的非营利组织工作。 近年来,我们测试了许多 EMM 产品。尽管我们只需要一些非常基本的功能,除了一两个特殊功能,但没有一个能真正赢
我已经在我的网站上安装了 Google 标签管理器,但自从新版本的 Google 标签管理器以来,我无法使用预览选项。每次我点击它时,我都会看到我的网站页面打开,但随后出现以下错误:“Tag Assi
我是 django 的新手,并创建了一个与教程中描述的民意调查网站没有太大区别的应用程序。 在网站上我得到: Exception Type: TemplateSyntaxError Exception
https://cloud.google.com/deployment-manager/docs/configuration/templates/create-basic-template 我可以像这
我们正在使用 Microsoft 的发布管理将我们的 Web 应用程序部署到我们的测试环境 (QA)。它是一个直接的 MVC.Net Web 应用程序。我们的构建生成一个 web 部署包,我们有一个命
我想将 python manage.py 缩短为 ./manage.py。 这可能很简单,但我找不到答案。我在有关 django 的问题的答案之一中看到了一步一步的方法,但我没有记住。尝试在 stac
我想将 python manage.py 缩短为 ./manage.py。 这可能很简单,但我找不到答案。我在有关 django 的问题的答案之一中看到了一步一步的方法,但我没有记住。尝试在 stac
我正在使用安装了 SQL Server Data Tools 的 VS 2012。我有一个 ADO NET 源,它使用 .Net Providers\MySQL 数据提供程序,并试图将一些数据推送到
根据我从文档中阅读的内容 https://developer.android.com/topic/libraries/architecture/workmanager , 它说: The task i
这两个类显然是相关的。 SupportFragmentManager 是否用于使用 FragmentTransaction 生成的 Fragments,而“常规”FragmentManager 专门用
我有一个桌子经理(经理ID、姓名、地址、城市、电话)。如果多个经理来自同一城市,我必须显示城市、姓名和电话详细信息。我的代码是:。但这向我展示了第一行中的一个错误,即“不是按表达式分组”。请救救我!
我是一名优秀的程序员,十分优秀!