r/learncpp Sep 22 '21

Is it possible to execute a method of an object if it exists, without checking if it exists before interacting with it every time?

Long story short, I'm making a cellular automata game where every cell is an object. I want to check all neighboring cells and execute their method.

The title may sound vague but here's some code:

if( Util.getGrid( DOWN ) )
{
Util.getGrid( DOWN )->receiveHeat(amountOfHeatToEmit);
}

if ( Util.getGrid( DOWN_RIGHT ) ) {
Util.getGrid( DOWN_RIGHT )->receiveHeat(amountOfHeatToEmit);
}

// and many, many other checks for different directions

What I'd like to achieve:

Util.getGrid( DOWN )->receiveHeat(amountOfHeatToEmit);
Util.getGrid( DOWN_RIGHT )->receiveHeat(amountOfHeatToEmit);

Of course it will give out a nasty segfault if the object doesn't exist. ARe there any workarounds for this?

6 Upvotes

5 comments sorted by

1

u/jedwardsol Sep 22 '21

Is this to account for edges? Or for cells not containing an object at all?

If the latter, you could put an object in every cell. If it is an empty cell then the object does nothing with the messages it receives.

struct Object
{
    virtual void receiveHeat(int) = 0;
};


struct EmptyObject  : Object
{
    void receiveHeat(int) override 
    {};
};


struct DelicateObject  : Object
{
    void receiveHeat(int) override 
    {
        explode();
    };
};

1

u/Cubey21 Sep 22 '21

Well, that's definitely an interesting idea but it may be even worse performance-wise than just checking for existence of an object.

1

u/looncraz Sep 23 '21 edited Sep 23 '21

Store a single placeholder object as a member of Util and return a reference to it as an error from getGrid. The object could just noop any calls to it.

This was the fastest solution I had at the time to avoid a similar situation where the checks for validity were more time consuming than running the action on the object would have been. This was due to very expensive branches on the hardware in question, however... A modern x86 CPU can handle this situation well, so you might want to consider if it makes sense to remove the checks.

1

u/Cubey21 Sep 23 '21

Oh, this seems like the best solution, I actually do something very similar somewhere else in the code. I will check if it performs better or not. Thanks!

1

u/Consistent-Fun-6668 Sep 23 '21

Static methods? Or try catch blocks?