473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Standard containers, polymorphic objects, and memory management.

I'm trying decide on the best way to structure the memory management in
my program.

I have a class (lets call it World), which contains a collection of
Entity objects.

Entity in turn, contains a pointer a Shape object. Shape is a pure
virtual class.

Now, I'm not sure who should "own" the allocation and deallocation of
the Shapes. It makes sense that World should own the Entity objects
directly, but it seems to complicate matters if I want the Entity
objects to delete (through auto_ptr, or on there own destructor) the
Shape *.

Also, I'd prefer (although its not a requirement) to avoid the "new"
operator for a lot of this. eg, I'd like to do something like this:

World world;
world.addEntity(Entity(new Sphere(sphereParams)));

where you have:
void World::addEntity(Entity &entity) {
entities.push_back(entity);
}

Now, if Entity deletes its shape on destruction, I need to make sure
that the Entity copy constructor uses move semantics, but I'm not sure
that's compatible with std::vector<Entity>. Or I need to make World
accept (and manage) Entity pointers.

I suppose I could also make it so that World manages the Shape object
life-cycle, eg "Shape &shape World::addShape(Shape *shape);".

So, any suggestions on which route I should take?

Thanks,
Daniel.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Jun 27 '08 #1
2 1429
I have a class (lets call it World), which contains a collection of
Entity objects. Entity in turn, contains a pointer a Shape
object. Shape is a pure virtual class.
Your description looks like this:

class Shape
{
// one or more pure virtual functions

};

class Entity
{
std::auto_ptr<Shapes_; // or shared_ptr<Shape>
// ...

// has copy ctor & assignment op
// by either cloning or sharing the pointee.

};
class World
{
std::vector<Entityentities_;
// ...
};

So assuming you have a single World instance, memory management seems
to be taken care of.
Now, if Entity deletes its shape on destruction, I need to make sure
that the Entity copy constructor uses move semantics, but I'm not
sure that's compatible with std::vector<Entity>. Or I need to make
World accept (and manage) Entity pointers.
You can ensure that Shape has clear clone semantics or use shared_ptr
idiom to make it compatible to vector<Entity>. I would advise against
use of raw pointers.

-Amal
Jun 27 '08 #2
On Jun 3, 4:36 am, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.netwrote:
I'm trying decide on the best way to structure the memory
management in my program.
I have a class (lets call it World), which contains a
collection of Entity objects.
Entity in turn, contains a pointer a Shape object. Shape is a
pure virtual class.
Now, I'm not sure who should "own" the allocation and
deallocation of the Shapes. It makes sense that World should
own the Entity objects directly, but it seems to complicate
matters if I want the Entity objects to delete (through
auto_ptr, or on there own destructor) the Shape *.
Be careful with the concept of ownership. In most applications,
it doesn't really make sense to have a "world" object which owns
the entity objects, but rather the let the entity objects "own"
themselves. And do Shape objects really require destruction; do
they have a deterministic lifetime. If not, then the simplest
solution is to use a garbage collector, and not worry about it.

Otherwise, I don't think that there's any real problem in having
Entity be responsible for the lifetime of its Shape. But of
course, this will really depend on the actual semantics of the
objects in question. There is no one rule which fits all cases.
Also, I'd prefer (although its not a requirement) to avoid the
"new" operator for a lot of this.
If you're objects are polymorphic, you don't have much choice.
eg, I'd like to do something like this:
World world;
world.addEntity(Entity(new Sphere(sphereParams)));
where you have:
void World::addEntity(Entity &entity) {
entities.push_back(entity);
}
Why? Entity objects typically have identity, which means that
they don't support copy or assignment; they usually also have an
arbitrary lifetime. Both of which mean that they should
probably never be allocated other than dynamically.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Gandalf | last post by:
Hello. I have some questions about the standard containers. How does the standard containers behave if I do queue<Foo> myQ; queue<Foo> myQ2; .... insert into myQ... myQ = myQ2;
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
4
by: Vincenzo Cappello | last post by:
I need a map that contain different object of the same base class like: std::map< int, base_class* > someone tell me is no correct using pointer in containers so i change to: std::map< int,...
35
by: dragoncoder | last post by:
Just a simple theoritical question to the experts. What was the rationale behind making STL containers follow copy semantics rather than reference semantics. References almost always make things...
21
by: George Exarchakos | last post by:
Hi everyone, I'd like your help... Can we have a std::list<BASEwhere BASE be the base class of a class hierarchy? I want to add to this list objects that are inherited from BASE class but not...
2
by: Arash Partow | last post by:
Hi all, I've got a question related to emulating aspects of polymorphism with CRTP. Below is a typical polymorphic class hierarchy with a definition of a "somewhat" heterogeneous container of...
7
by: mscava | last post by:
Hi... Today I was trying to find the best way to handle elements in Containers. I've found 3 appraoches but all do have pros and cons... 1. std::vector<MyClass> - inefficient - you have to...
14
by: Remo D. | last post by:
I've read with much interest the threads on how to create data containers in C. I started thinking if simpler ADT (other than the proposed List) could be used as a starting point. What about...
3
by: massysett | last post by:
Greetings, Having classes with member objects that have STL containers of objects whose definitions are incomplete results in undefined behavior. See for example: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.