#### Archived

This topic is now archived and is closed to further replies.

# Pointers to member functions

This topic is 5389 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

int main()
{

}

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

##### Share on other sites
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]

##### Share on other sites
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

##### Share on other sites
Thanks, i will check both of those out, and i also appretiate the insight.

EOF

1. 1
2. 2
JoeJ
18
3. 3
4. 4
frob
12
5. 5

• 13
• 16
• 13
• 20
• 13
• ### Forum Statistics

• Total Topics
632188
• Total Posts
3004650

×