CS Dude
CS Dude

Reputation: 451

Any instance access all instances (of a class)

This may seem like a trivial question, or I may have misunderstood previous information/the research I've done so far.

But is it possible to have a object with a function (in C++) that can access all instances of its own type?

In the context of my usage. I wanted to have a Button class, whereby I could simply instantiate multiple Buttons but call to a function could call reference all buttons.

ButtonInstance.isMouseTargetting(cursorCoordinates);

Is this possible? If so is it efficient?

Or should I have the class which owns the Button instances call each instance to check if the mouse coordinates match up?

Upvotes: 0

Views: 1117

Answers (4)

rkachach
rkachach

Reputation: 17355

The short answer is yes. But i will not recommend to put this functionality on the Button class since this will add extra (maybe not expected) responsibility to it. You can achieve the desired behavior by storing your Button objects on some collection and then call a function to check which button is targeted by the mouse.

Another solution would be to store the buttons collection as a member of a higher level class that represents your user-interface. This way you can call a method of this class and check if the mouse cursor is currently on some Button or not. With this design you can add the same support for other GUI elements (if you need to) easily.

Upvotes: 0

Captain Giraffe
Captain Giraffe

Reputation: 14705

I'm under the impression you are looking for advice on how to design this.

In the context of my usage. I wanted to have a Button class, whereby I could simply instantiate multiple Buttons but call to a function could call reference all buttons.

You want to do this in a button container. A button is not a button container and in a GUI context you already have an established hirerarchy.

Or should I have the class which owns the Button instances call each instance to check if the mouse coordinates match up?

Yes. You probably already have a window/container class for this.

Upvotes: 1

aslg
aslg

Reputation: 1974

You can, for example, make a list of all objects created for a given class,

class Button {
public:
    Button() {
        _buttonList.push_back( this );
        // assign index/handler to this button
    }
    ~Button() {
        _buttonList.erase( _handle );
    }

    static bool isMouseTargeting( float x, float y ) {
        for ( auto button : _buttonList ) {
            // check if inside
        }
        return false;
    }

private:
    static std::list< Button* > _buttonList;
    // Handler _handle;
}

This is only a very general example of what you could do. You can use any other container besides a list (entirely up to you), and you have to find a way to index each button (or create a handle) so that you can later erase it in the destructor.

Beware of the default constructors (copy or move). If you don't explicitly create your constructors then some of your buttons will not enter the list, so either make them yourself or delete them.

Button( const Button& button ) = delete;

This is one way to do what you asked, but not necessarily the best solution. It may be simpler to just add the buttons to a non-static container by yourself and search from there.

Upvotes: 0

Misgevolution
Misgevolution

Reputation: 845

Your question is more of about Design pattern than C++ itself. Take a look at the Gang of Four book;you will find an appropriate implementation.

Upvotes: 0

Related Questions