473,738 Members | 3,636 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer to Member Function of derived class

<code>
class Base {
};

class Class1 : public Base{
public:
void Function1();
}
void Tracker(Base b, void (Base::*callbac k)());

int main(){
Class1 o1;
Tracker(&o1, &Class1::Functi on1);
}
</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 1509
On May 24, 8:14 pm, Vulcan <vulcan.ea...@g mail.comwrote:
<code>
class Base {

};

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

}

void Tracker(Base b, void (Base::*callbac k)());

int main(){
Class1 o1;
Tracker(&o1, &Class1::Functi on1);}

</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**********@g mail.comsaid:
On May 24, 8:14 pm, Vulcan <vulcan.ea...@g mail.comwrote:
><code>
class Base {

};

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

}

void Tracker(Base b, void (Base::*callbac k)());

int main(){
Class1 o1;
Tracker(&o1, &Class1::Functi on1);}

</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::Functio n1 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::*callbac k)());

int main(){
Class1 o1;
Tracker(&o1, &Class1::Functi on1);
}
</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<typena me T>
void Tracker(T &base, void (T::*callback)( ));

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

template<typena me callback>
void Tracker(callbac k &c) { c(); }

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

}
int main() {
Class1 o1;
Tracker(Class1F ucntion1Callbac k(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
5637
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
12205
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
7925
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
1525
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
5005
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 signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
1
1679
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 type) in the derived class. I know in Visual C++ you can just cast the pointer to member function of the derived class to that of the base class and it will work, but is this legal by C++ standards?
6
2626
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
3813
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
3184
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
3827
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 event); so I declared a function pointer like typedef void (CFObject::*CFEventHandler)(CFEvent *theEvent);
0
8969
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9476
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9208
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.