O_o
First time I do understand what you say.
Yay!
OK, some context might be good then.
I am thinking of something like this...
First, a class that demonstrates many of the questions I ask. Class BodyPart (some unrelevant parts cut):
Code:
class BodyPart
{
public:
BodyPart(const std::tr1::shared_ptr<Texture>& tex, float gPoint_x, float gPoint_y);
virtual ~BodyPart();
private:
Coordinates m_coord;
std::tr1::shared_ptr<Texture> m_tex;
hgeSprite m_spr;
GluePoint gPoint;
};
2) BodyPart is supposed to be a generic class for "body parts" of a sprite. Hence, it wants a texture. Since it's generic, it doesn't create the texture, but rather accepts it from the user and creator of the class.
The idea is that BodyPart shall own the texture because it's the one that will operate and use the texture. Once the body part is destroyed, so shall its associated texture be, too.
So what type should BodyPart's constructor accept? And what type should it store among its members?
1) Assume, for a second, that BodyPart could allow outside access to its texture. Since BodyPart owns the texture (or should), we don't want to return a shared_ptr since that would indicate shared ownership (plus the chance that the texture might outlive its owner). So what type should BodyPart return then, if this was possible? weak_ptr?
3) First, an example:
Code:
class GluePoint
{
public:
virtual void Connect(GluePoint& point);
private:
no_delete_ptr<GluePoint> partner;
};
GluePoint is instantiated as seen above, as a member of the BodyPart class.
It needs to communicate with another GluePoint and thus it wants to "connect" to it, and store a pointer to that other instance of GluePoint.
Because the other GluePoint will also be located inside a BodyPart, they are not created on the heap and thus cannot be deleted.
GluePoint just wants to acquire a non-owning instance (because it doesn't own its "partner" nor should it destroy it). I solved it by using a no_delete_ptr, which is basically just a thin wrapped around a pointer, but the name of the container is supposed to give a message: do not delete this pointer.
Implementation:
Code:
void GluePoint::Connect(GluePoint& point)
{
partner = &point;
}
So what type should GluePoint::Connect accept, and possibly what type should it store to make a connection like this possible? That is my question.
I won't say this design is flawless or good, just the way it is now, and I'm scrambling for ideas on how to manage ownership.