C++ : objects with the same function

Started by
7 comments, last by Narf the Mouse 12 years, 3 months ago
First of all I want to wish everyone a merry christmas and a happy new year. I've been learning C++ and SDL for some time now, but I can't seem to think of a way to call the same function from multiple objects ( of the same class, ofcourse) I am 100% sure there is a way so if someone can give me a hint, tutorial , an existing topic on this subject I will be more then happy.

Thanks in advanced
Advertisement
Hi Alex,

I don't really understand the question. Could you be more specific, and if possible post some code to illustrate your problem?

There is nothing stopping multiple instances of a class from calling the same function - in fact this is completely routine - you "just call it".

First of all I want to wish everyone a merry christmas and a happy new year. I've been learning C++ and SDL for some time now, but I can't seem to think of a way to call the same function from multiple objects ( of the same class, ofcourse) I am 100% sure there is a way so if someone can give me a hint, tutorial , an existing topic on this subject I will be more then happy.

Thanks in advanced

Do you mean a static member function? (or static method... as you prefer to named it..)... otherwise try to be more specific : )
Well I am on my laptop and I dont have code ready but this is what I actually tryed to do: I made a class named Button and in it there is a function that handles events aka I wanna check if the mouse's X and Y are in the area of the button but I dont wanna call the same function for each button respectivly so I was asking if there is a way to actually call the event function on all the Buttons at once.

class Button{
handle_event()
}


simple example. I am using SDL but I am pretty sure that it wont help much
Have you tried using an event handler class? This is especially useful if you linked or created the button objects using an array.


class Button
{
void HandleEvent();
};

class EventHandler
{
EventHandler()
{
button_array = new Button[10]; // Create 10 buttons (You can't use the 10th place if I'm not wrong, this may be visual studio specific)
}

~EventHandler()
{
delete [] button_array;
}


void HandleButtonEvents()
{
for (int x = 0; x < 10; x++) {
button_array[x].HandleEvent(); // Iterate through the array of buttons and call their respective HandleEvent function
}
}

Button * button_array;
};


That is the basis of the solution you're looking for. You'll have to modify this so that you're able to add/remove buttons and etc.

...I was asking if there is a way to actually call the event function on all the Buttons at once.

Not really. One way or another you need to call them all one at a time. There are ways to simplify it, like storing pointers to all your buttons in a container and then looping over the container, but eventually you'll need to call them one at a time.
May be this.
class base
{
public:
base(void){}
~base(void){}
type DoStuff(params){return 1;}
};


class DerivedclassA, public base
{
public:
DerivedclassA(void){}
~DerivedclassA(void){}

type DoStuff(params){return 1;}
this sux had a post going pressed the tab and lost it now I give up..............
Ok I put in in note pad first this time lol.

class base
{
public:
base(void){}
~base(void){}
virtual bool DoStuff(int val);
{return true;}
};//end class

class DerivedClassA: public base
{
public:
int count;
DerivedClassA(void){}
~DerivedClassA(void){}
virtual bool DoStuff(int val)
{
count++;
return true;
}
};//end class
used like so.
base *unit = 0;
DerivedClassA car;
unit = (base *)&car;
unit->DoStuff(1);//use the base class pointer
look up derived objects inheritance.
it is always save to convert a derived object to a base object because there is always an instance of the base class in the derived class.
Note: "car" is created in-context and will be destructed once that context exits (generally speaking, if it's between { }, it's a context). So, if you intend to use "unit" anywhere outside of that context, it'll point to a destructed object.

You may or may not know that, but it's worth pointing out - No pun intended.

This topic is closed to new replies.

Advertisement