Pointers to member functions

Started by
2 comments, last by MrBeaner 20 years, 5 months ago
Hello- OK... i am trying to create a GUI for SDL. everything is fine, except one thing: if i try to create a pointer to a member function from another class, i have to declare the function like so:

class s
{
public:
s(void(t::*s)(void)){
pfunc = s;
}
protected:
void (t::*pfunc)(void);
};

class t
{
public:
t(){
NewS = new s(save);
}
~t(){delete NewS;}
void save(){};

protected:
s* NewS; //whatever

this works, because class s is expecting a pointer from class t. However, this is not very expandable. I can use templates, but if i want to use a non-member if breaks. For example, if i tried to implement the code above like this:

template <class type>
class s
{
public:
s(void(type::*s)(void)){
pfunc = s;
}
protected:
void (type::*pfunc)(void);
};

class t
{
public:
t(){
NewS = new s<t>(save);   //still works

}
~t(){delete NewS;}
void save(){};

protected:
s* NewS; //whatever

};

//floating function

void load(){};

int main()
{
s<load> SuperS;   //this doesn''t work.

}
does anybody know of a way to do what it is i am trying to accomplish, or should i use templates and forget about floating functions (i mean, i am using objects, and technically i shouldn''t have this problem, but i would like to offer the ability, if possible. thanks for your help... EOF
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
Advertisement
Ive always considered myself fairly strong with C++ and C in general though im not a pro (short bio)

In C i used pointers to functions alot in games. First came to terms with em in quake2 modding and really liked em.

However in C++ ive never gotten pointers to class functions to work for me without giving me headaches. I just gave you my bio cause that just might be a weakness in my knowledge.

HOWEVER.

There is something that alot of text use that is just is strong because they allow all the same stuff that pointers to functions did. They are called functors. All a functor is is a class with the operator() overload. Its also virtual.


So for example you would have.
class Think
{
public:
virtual void operator()(Entity& e, World& w) = 0;
}

Class Camper_Think : public Think
{
//this is the think function for things with the camper mentality
public:
virtual void operator()(Entity& e, World& w)
{//do camper stuff ;}
}

class Bot : public Entity
{
//bots can have different personalities. We reflect that by having their thinking be different.
Think* thinker;
}


Now when you use Bot
you can make many bots via new
and you can load up some of em with
current_bot->thinker = giant_brain->mp_camper_think
OR
if you had other thinkers you could do
current_bot->thinker = giant_brain->mp_chaser_think

All in the same code.

Now the code thats using that starts iterating through the bots and it doesnt have to know what the actual think is cause its virtual it just does
(*current_entity->thinker)(*current_entity, world);

just like that. And it looks alot like a point to a function doesnt it?

But its not its a pointer to a class. AND ITS SOOOO MUCH EASIER THEN POINTERS TO FUNCTIONS IN CLASSES.

NOw is this the best way to model that?
Naw cause bots dont change thinking process alot mid game. I mean why not just inherit from the entity itself. But the cool reason you might not do that is the functors could developed in a dll later without rebuilding the code that used em. Thats pretty =)

Someone might note that pointers to classes with virtual functions sounds slower then pointers to function. It is indeed. If this is a choke point in your code this is not good. But as far as being a way to organize selections for various behaviors.. rarely speed critical in my limited experience. Anything that helps you organize it easier is a win to me =)








[edited by - declspec on November 18, 2003 2:08:55 AM]
Try looking at the boost libraries at www.boost.org

Included in the library, there are;
boost::mem_fn,
boost::bind and
boost::function

They take a little getting used to but once you get the hang of them they will cover virtually any requirements.

Hope this helps
Thanks, i will check both of those out, and i also appretiate the insight.

EOF
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.

This topic is closed to new replies.

Advertisement