关于如何处理重置静态类中的数据成员对象的建议 [英] Recomendations on how to deal with reseting a data member object in a static class

查看:49
本文介绍了关于如何处理重置静态类中的数据成员对象的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类A,它是一个单例(通过使用著名的Modern C ++所建议的静态变量来实现,以避免内存泄漏),其中包含类B的数据成员. B类基本上是代表树结构中的节点的类,该树包含我在屏幕上绘制的3d骨骼的关节信息.该类实现了复制构造函数和=运算符,因为我在STL向量中使用了它(实际上将每个节点的子代存储在一个向量成员中,因为一个节点可能有任意数量的子代).

该树结构是由类A中的方法创建的,该方法加载一个文件,其中包含骨架的数据.解析文件后,它随后调用一个递归帮助器方法,该方法创建新节点并根据需要返回新对象的副本.一切运行都很好,没有问题,但是现在我想向A类添加一个卸载方法.如果我是对的,我可以通过三种方法实现这一目标,但是我不确定哪种方法最好:

1.我可以在B类中创建一个称为reset的方法,该方法将删除所有子级并将每个成员重置为其原始默认状态,并为存储在A类中的root元素调用它.这可能会解决我的所有问题,并另外给出如果我想扩展并为用户提供一种摆脱特定关节的方法,而不仅仅是推翻根部,我可以使用新功能.但是不知何故,我觉得这不是一个好的设计实践,因为您知道使用一种公开的方法
2.我可以只使用=运算符,然后将B类的新副本分配给我的根元素.
3.研究是否有办法从静态类(类A)中销毁"一个成员,因为保存根类B元素的数据成员根本没有分配在堆中,而只是分配了一个副本,所以我想知道如果可能的话.

I have a class A that is a singleton (implemented by using a static variable as suggested by the famous book Modern C++ in order to avoid memory leaks) which contains a data member of type class B among other stuff. Class B is basically a class that represents a node in a tree structure which holds information on joints of a 3d skeleton that I’m drawing on screen, this class implements the copy constructor and = operator since I use it in STL vectors (I actually store the children of each node in a vector member cause there might be any amount of children for a node).

The tree structure gets created by a method in class A that loads a file which contains the data for the skeleton. After parsing the file it then calls a recursive helper method that creates new nodes and returns a copy of the new object as required. Everything runs really nice with no problems but now I want to add an unload method to class A. If I’m correct I have three ways to achieve this, but I’m not sure which would be the best:

1. I could create a method called reset in class B which would get rid of any children and reset every member to its original default state and call it for the root element stored in Class A. This would probably solve all my problems and additionally give me new functionality in case I want to expand and provide the user a way to get rid of particular joints not just nuke the root. But somehow I get the feeling this wouldn’t be a good design practice, you know having a public method that
2. I could just use my = operator and assign a new copy of class B to my root element.
3. Investigate if there is a way to "destroy" a member from a static class (class A) , since the data member that holds the root class B element is not allocated in heap at all but just gets a copy assigned then I wonder if this is even possible.

Thanks for your comments in advance.

推荐答案

就像您的loader函数一样,您可以在A中使用unloader函数.作为您的第一个解决方案,可以解决您的问题,只是想为此加一点.将重置功能设为B中的私有功能(如您所说,它将使任何孩子乘车,并将每个成员重置为其原始默认状态),并让您的朋友成为A类.这样做是您遇到的公共方法"问题将会过去.从A的卸载程序函数调用此私有函数.

希望这对您有用.
As like your loader function you can have an un-loader function in A. As your first solution can solve your problem, I''d just like to add a point to that. Make the reset function private in B (which as you said, will get ride of any children and reset every member to it''s original default state) and friend to class A. By doing this your problem of ''public method'' will be over come. Call this private function from the un-loader function of A.

I hope, this will work for you.


这篇关于关于如何处理重置静态类中的数据成员对象的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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