473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pointer to Member Function of derived class

<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.
Jun 27 '08 #1
3 1494
On May 24, 8:14 pm, Vulcan <vulcan.ea...@gmail.comwrote:
<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().
Jun 27 '08 #2
On 2008-05-24 11:47:18 -0400, Vulcan <vu**********@gmail.comsaid:
On May 24, 8:14 pm, Vulcan <vulcan.ea...@gmail.comwrote:
><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)

Jun 27 '08 #3
Vulcan wrote:
<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/>
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Abhijit Deshpande | last post by:
Is there any elegant way to acheive following: class Base { public: Base() {} virtual ~Base() {} virtual void Method() { cout << "Base::Method called"; return; } };
4
by: Bren | last post by:
Hi all, Given this situation: class Base { typedef void (Base::*FN_FOO)(); virtual void Foo() = 0; // pure virtual void GetFoo(FN_FOO pfnFoo) = 0; // pure virtual
6
by: xsist10 | last post by:
whats the best method. delphi lets you do something very simple ptrMethodFoo : procedure how do you do this in C++?
7
by: christopher diggins | last post by:
Hi everyone, I have the following code (greatly simplified to demonstrate issue) : #include <iostream> class CBaseFuBar { public: virtual void Fu() { std::cout << "base"; }; };
37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
1
by: none | last post by:
Hi, I have a base class with a pointer-to-member function variable. Then I have a derived class that needs to use that variable to call a member function (with the same arguments and return value...
6
by: marco_segurini | last post by:
Hi, the following sample code shows a compiler error I get trying to build some old code with the last CL compiler (vers 13.10.3077): //----- begin #include <iostream> namespace ns {
7
by: WaterWalk | last post by:
Hello. I thought I understood member function pointers, but in fact I don't. Consider the following example: class Base { public: virtual ~Base() {} }; class Derived : public Base {
3
by: Dmitry | last post by:
Hi all, Consider the following code: class A { public: A() {} void DoMethod() { (this->*m_pMethod)(); } protected: virtual void MethodA() { ... }
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.