Sign in to follow this  

What is a best practice to allow one instance of object?

This topic is 1280 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

You can see in the code snippet, m_crowd is allocated as many times as different derived objects are created. If I make the object static, the m_crowd will be the last copy of allocation. I make a base class because I want to make a generic container so that all objects can be tested for collisions. Am I better off to make this variable global? do I loose the cohesion feature of OOP if I do so? Thanks Jack
class OgreDetourCrowdBase {
   OgreDetourCrowdBase();
   dtCrowd *m_crowd;
};

class OgreDetourCrowdCB : public OgreDetourCrowdBase {
   OgreDetourCrowdCB();
};

class OgreDetourCrowdVNA : public OgreDetourCrowdBase {
   OgreDetourCrowdVNA();
};

OgreDetourCrowdBase::OgreDetourCrowdBase()
{ 
    m_crowd = dtAllocCrowd();
}

Share this post


Link to post
Share on other sites

If you make it static, then you don't want to allocate and overwrite it every time you create an object. Allocate it once at startup at the appropriate time, or make it an object and let it manage its own lifetime if that's possible.

Share this post


Link to post
Share on other sites
I'm probably missing something, but your question is asking about collision tests.

That is normally done by adding information to a collision system, not by having a static objects that everything must belong to.

Trying to make it a part of the design where all objects self-register is fairly dangerous. All objects must self register. There is no way to make an object that doesn't self register. If you are passing parameters or making an array, every temporary object is absolutely required to self register. If you are making an off screen object it still must register for the collision system. If you are making an object and it is not part of the world yet, it still must register for collisions. In such a design, this is mandatory. You cannot create any object, for any duration, without it automatically getting registered.

A typical approach is that when things get added to the world (not object construction) you also create zero or more physics objects which get included for collision detection and response. When an object is removed from the world (not object destruction) you also remove any associated physics objects.

Share this post


Link to post
Share on other sites

This topic is 1280 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this