静态方法创建一个对象而不是构造函数 [英] Static method to create an object instead of constructor

查看:201
本文介绍了静态方法创建一个对象而不是构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C ++应用程序中创建一个GUI,我有一个类叫做$ code> GUIObject ,它是所有其他组件的基类,例如按钮 CheckBox 窗口



我还有一个类 GUIObjectsStorage ,它由创建的所有 GUIObject 组成。到目前为止,我一直在使用原始指针,所以我刚刚为 GUIObject 类创建了这个构造函数:

  GUIObject :: GUIObject():
{
GUIObjectsStorage :: Instance()。addObject(this);
}

这对我的需要是确定的,因为每当我想访问特定对象,我刚从 GUIObjectsStorage 中取出。但是现在我正在努力使用智能指针,所以 GUIObjectsStorage 现在存储 std :: shared_ptr< GUIObject> 而不是raw指针,我不能使用我之前使用的构造函数:

  GUIObject :: GUIObject ():
{
GUIObjectsStorage :: Instance()。addObject(std :: shared_ptr< GUIObject>(this));
}

因为例如:

  //代码中的某个位置
std :: shared_ptr< Button> bt = std :: shared_ptr< Button>(new Button());

基本上我现在有两个 shared_ptr s(一个在这里,第二个在 GUIObjectsStorage 中,因为它被添加在 Button 的构造函数中),它们都有引用计数= 1,但都指向内存中的同一个对象。然后,如果其中一个死机,对象本身也被销毁。



所以我想出了一个想法,可能会为从<$ c $继承的所有类创建私有构造函数c> GUIObject 并创建一个静态方法,它将创建并返回 std :: shared_ptr ,它的副本添加到 GUIObjectsStorage 。这样我可以使用参考count = 2的 shared_ptr s:

  class Button:public GUIObject 
{
private:
// ...
Button();
public:
// ...
static std :: shared_ptr< Button>创建();
}

std :: shared_ptr< Button> Button :: create()
{
std :: shared_ptr< Button> bt = std :: shared_ptr< Button>(new Button());
GUIObjectsStorage :: Instance()。addObject(bt);

return bt;
}

通过隐藏构造函数,我可以确定没有人会以不同的方式创建一个对象而不是使用 create()方法。



但这是设计这个好的方法吗?如果没有,对于这个问题可以做些什么呢?

解决方案

这是工厂制作对象的经典用法。



说,你可能不需要这个。你知道窗口小部件不再需要吗?像这样的GUI经理经常这样做。如果是这样,评论者是正确的:指定对象的所有者,让它删除它,并设置好。


I'm creating a GUI in my C++ application and I have a class called GUIObject which is base class for all other components, like for example Button, CheckBox, Window etc.

I also have a class GUIObjectsStorage, which consists of all GUIObjects that are created. So far I've been working with raw pointers, so I just had this constructor for GUIObject class:

GUIObject::GUIObject() :
{
    GUIObjectsStorage::Instance().addObject(this);
}

And it was ok for my needs, because whenever I wanted to access specific object, I just took it from GUIObjectsStorage. But now I'm trying to move into use of smart pointers, so that GUIObjectsStorage now stores array of std::shared_ptr<GUIObject> instead of raw pointers and I can't use my constructor as I used it before:

GUIObject::GUIObject() :
{
    GUIObjectsStorage::Instance().addObject(std::shared_ptr<GUIObject>(this));
}

because for example:

// Somewhere in code
std::shared_ptr<Button> bt = std::shared_ptr<Button>(new Button());

Basically I'd now have two shared_ptrs (one here, second in GUIObjectsStorage, because it was added in Button's constructor) which both have reference count = 1, yet both point to the same object in memory. Then if one of them dies, object itself is being destroyed too.

So I came up with an idea to maybe make private constructor for all classes inheriting from GUIObject and create a static method which would create and return std::shared_ptr and it's copy add to GUIObjectsStorage. This way I could have shared_ptrs with reference count = 2 which is correct:

class Button : public GUIObject 
{
private:
    // ...
    Button();
public:
    // ...
    static std::shared_ptr<Button>  create(); 
}

std::shared_ptr<Button> Button::create()
{
    std::shared_ptr<Button> bt = std::shared_ptr<Button>(new Button());
    GUIObjectsStorage::Instance().addObject(bt);

    return bt;
} 

By hiding constructor I could be sure that nobody will create an object in different way than by using create() method.

But is this good way of designing this? If not, what could be a better solution for this problem?

解决方案

The is a classic use of a Factory for making objects.

That said, you might not need this. Do you know when widgets are no longer needed? GUI managers like this often do. If so, the commenter is right: designate an owner of the object, let it delete it, and you're set.

这篇关于静态方法创建一个对象而不是构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆