gtkmm管理/添加与智能指针: [英] gtkmm manage/add vs smart pointers:

查看:95
本文介绍了gtkmm管理/添加与智能指针:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gtkmm使用以下结构提供小部件的生命周期管理:

gtkmm provides lifetime management of widgets using this sort of construct:

Gtk::Widget* aWidget = Gtk::manage(new Widget()); 

Gtk::Widget containerWidget;

containerWidget.add(*aWidget);

这将aWidget的生命周期管理委托给containerWidget.清理containerWidget时,还会清理aWidget,类似于Delphi的所有者"概念.

This delegates lifetime management of aWidget to containerWidget. When containerWidget is cleaned up, it also cleans up aWidget - similar to Delphi's 'owner' concept.

我们还有几种类型的智能指针,特别是C ++ 11智能指针模板,我在各处使用它.

We also have several types of smart pointers, particular the C++ 11 smart pointers templates, which I use everywhere.

我发现Manage/add语法更容易使用,更干净,但它不是C ++标准的一部分,它是gtkmm的特定功能,这使我认为我应该坚持使用std :: shared_ptr等.

I find the manage/add syntax easier and cleaner to use, but it's not part of the C++ Standard, it's a gtkmm specific feature, which makes me think I should stick to std::shared_ptr, etc.

所以我想知道std智能指针与gtkmm管理/添加模型的优点/缺点(除了在删除所有者容器后需要引用的情况,或者当您具有顶层小部件时)没有包含窗口小部件).

So I'm wondering what are the advantages/disadvantages of std smart pointers vs the gtkmm manage/add model (aside from cases where you need the reference after the owner container has been deleted, or when you have a top level widget that has no containing widget).

推荐答案

如果您在添加小部件后无需保留对该小部件的引用,则我认为将shared_ptr用于小部件并不实用.最好使用托管窗口小部件,而不要使用shared_ptr.

I don’t think it's practical to use shared_ptr for widgets if you don't need to keep a reference to that widget after you add it. You are better off having a managed widget instead using shared_ptr.

关于gtk :: manage的好处是,您可以在堆上创建对象,然后将其添加到容器中,而不必理会它,而当该容器被破坏时,它将破坏小部件.

The nice thing about the gtk::manage is that you can create the object on the heap and add it to the container and forget about it, and when that container is destroyed it will destroy the widget.

通常,如果将小部件添加到容器中后不需要引用它们,则创建受管理的小部件;如果确实需要引用它们,则分配一个指针.

As a general rule I create my widgets managed if I don't need to reference them after I add them to a container, if I do need to reference them, I allocate a pointer.

这篇关于gtkmm管理/添加与智能指针:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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