Archived

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

FatalError

Pointer to a derived class function

Recommended Posts

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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think I sorta understand but...
I think most reponses are going to be "Why do you need to do this?". From my experience if you have difficult explaining what it is you need to do then there is usually an easier design.

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

}
};

Share this post


Link to post
Share on other sites