Click here to Skip to main content
16,012,223 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.
Posted

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900