Skip to main content
gamedev.net
🔒 Locked

Pointer to a derived class function

Started by FatalError Aug 13, 2003 at 4:58 AM 6 replies 500+ views
Original Post
FatalError
FatalError
Hi, I have a little problem. Here''s what I have class obj class A { Obj obj; ... } class B : public A { void B::test() { } } I want to set in the obj of the class A a pointer to a function of class B. How can I do that? If I define in obj: void (B::*fp)(); then ok it works: I have a pointer to a function of class B But how can I do that so it fits to any of the derived class of A ? If I have class C : public A, how can I set the same pointer to a function of obj to point to a function of C ? I hope I made myself clear enough :-) Thanks
Mathematix
Mathematix
A base class cannot inherit the methods and members of a derived class. Only derived classes can inherit from the base class. In other words, your base class cannot see the derived class even though the derived class can see the base class.

Beginners and experts will find answers here.
ToohrVyk
ToohrVyk
Ok, I see. Well, if you examine the types, then clearly there is no way of doing it this way : since fp is defined as a B member function pointer, it can NOT point to anything else. And since a C member function is NOT a B member function, you can''t do this.

My solution : move on to OCAML, where the type-checking allows that kind of thing.

Or, do it another way. What are you trying to do exactly? There ought to be anopther solution, possibly with virtual functions.

ToohrVyk

FatalError
FatalError
ok, so I''ll have to find another way.
What I wanted to do is:
The Main class (A) is a Screen class
The derived class (B,C) are different type of screens
(intro,credits,... for example)

The main screen contains some controls (the obj here)
So I wanted to create a button in a screen and when it''s clicked I wanted the button to call the correct function of the correct class.

IntroScreen->addButton(10,20,Buttwhenclickedoned)
where Buttwhenclickedoned is a function of IntroScreen. So if the button is clicked, it calls this function.

That''s all.
Thanks for your responses
antareus
antareus
Use boost::function with boost::bind like so:

#include <boost/function.hpp>
#include <boost/bind.hpp>

// object that has some method we want to invoke

A a;

// this syntax is for MSVC 2003 only, consult docs for the 'compatible' syntax that works with MSVC6

// IntFunction will be a function returning void with a single int parameter

typedef boost::function<void (int)> IntFunction;

// bind takes a function to run, and then anything after that is considered an argument to that function

// here it is binding a member function (A::Test to the instance of the object it will be invoked on)

IntFunction callback = boost::bind(A::Test, &a, _1);

callback(42); // call a.Test(42)



(this is all off the top of my head, the docs are much more clear, but you get an idea how it works)

Check it out at http://www.boost.org/

Edit: boost::function provides operator(), so it doesn't need to be dereferenced like a conventional function pointer.

[edited by - antareus on August 13, 2003 1:31:45 PM]
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
rypyr
rypyr
I''m sure you could figure out a way to do this using virtual functions and interface classes. Here''s an example:


class IButtonHandler {
public:
virtual void buttonPressed() = 0;
};

class Button {
IButtonHandler* pHandler;
// ...

};

class Screen {
protected:
Button b;
};

class MenuScreen : public Screen, IButtonHandler {
public:
MenuScreen() : Screen() {
b.pHandler = this;
}
void buttonPressed() {
// do action here

}
};

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.