Jump to content
  • Advertisement
Sign in to follow this  
?????????????????

C++ : objects with the same function

This topic is 2537 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

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

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites

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 : )

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

...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.

Share this post


Link to post
Share on other sites
Hidden
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;}

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!