Connecting Tech Pros Worldwide Forums | Help | Site Map

Pointer to Member Function of derived class

Vulcan
Guest
 
Posts: n/a
#1: Jun 27 '08
<code>
class Base {
};

class Class1 : public Base{
public:
void Function1();
}


void Tracker(Base b, void (Base::*callback)());

int main(){
Class1 o1;
Tracker(&o1, &Class1::Function1);
}
</code>

The above code doesn't work because Function1 is only declared in the
derived class. Is there a way around this? I want to be able to have a
member function of a derived class as callback.



Vulcan
Guest
 
Posts: n/a
#2: Jun 27 '08

re: Pointer to Member Function of derived class


On May 24, 8:14 pm, Vulcan <vulcan.ea...@gmail.comwrote:
Quote:
<code>
class Base {
>
};
>
class Class1 : public Base{
public:
void Function1();
>
}
>
void Tracker(Base b, void (Base::*callback)());
>
int main(){
Class1 o1;
Tracker(&o1, &Class1::Function1);}
>
</code>
>
The above code doesn't work because Function1 is only declared in the
derived class. Is there a way around this? I want to be able to have a
member function of a derived class as callback.
I am looking for a way to provide a function Function1 with an object
Object1 and a member function of that object as a callback.

The full story:
I have several classes representing graphical objects like rectangle,
line, circle all derived from a common base say BObject. I need to be
able to modify any selected shape on the screen by dragging with the
mouse. I want to create one function called Tracker which will handle
mouse move events and update the shape on the screen by calling a
function on the object being modified.

Writing mouse tracking code per shape will take up too much repetitive
code. I felt it would be better for the tracker to have a callback
which it can call every time the mouse moves and then call ReDraw().
Pete Becker
Guest
 
Posts: n/a
#3: Jun 27 '08

re: Pointer to Member Function of derived class


On 2008-05-24 11:47:18 -0400, Vulcan <vulcan.eager@gmail.comsaid:
Quote:
On May 24, 8:14 pm, Vulcan <vulcan.ea...@gmail.comwrote:
Quote:
><code>
>class Base {
>>
>};
>>
>class Class1 : public Base{
>public:
>void Function1();
>>
>}
>>
>void Tracker(Base b, void (Base::*callback)());
>>
>int main(){
>Class1 o1;
>Tracker(&o1, &Class1::Function1);}
>>
></code>
>>
>The above code doesn't work because Function1 is only declared in the
>derived class. Is there a way around this? I want to be able to have a
>member function of a derived class as callback.
>
I am looking for a way to provide a function Function1 with an object
Object1 and a member function of that object as a callback.
>
Since Class1::Function1 is not a member of Base, you're going to be in
trouble if you try to call it on a Base object. That's why virtual
functions were invented.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Daniel Pitts
Guest
 
Posts: n/a
#4: Jun 27 '08

re: Pointer to Member Function of derived class


Vulcan wrote:
Quote:
<code>
class Base {
};
>
class Class1 : public Base{
public:
void Function1();
}
>
>
void Tracker(Base b, void (Base::*callback)());
>
int main(){
Class1 o1;
Tracker(&o1, &Class1::Function1);
}
</code>
>
The above code doesn't work because Function1 is only declared in the
derived class. Is there a way around this? I want to be able to have a
member function of a derived class as callback.
>
>
You might have to go the route of using a template for Tracker.
template<typename T>
void Tracker(T &base, void (T::*callback)());

Alternatively, you might want to have callback be a functor, and omit b
altogether:

template<typename callback>
void Tracker(callback &c) { c(); }

struct Class1Function1Callback {
Class1 &o;
Class1Function1Callback(Class1 &o) : o(o) {}
void operator() { o.Function1(); }

}
int main() {
Class1 o1;
Tracker(Class1Fucntion1Callback(o1));
}
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Closed Thread