473,791 Members | 2,901 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pass function point from child class to parent class

4 New Member
Hi,

How can a child class passes its non-static member function to the parent class?
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3. public:
  4. typedef void *(*object_t)(void *);
  5.    A(object_t f) : func(f) {}
  6.    virtual ~A(){}
  7. private:
  8.   object_t func;
  9. };
  10.  
  11. class B : public A
  12. {
  13. public:
  14.    B() : A(myFunc){}
  15.    ~B(){}
  16.    void *myFunc(void *){}
  17. };
  18.  
There was a compilation error of B() : A(&myFunc){}, unless the myFunc is to be a static member. How can do it without make myFunc be a static member?

Also, how can make the pthread_create to take a non-static member in class A? It was an error as well.

Thank you.

Jim
Aug 27 '07 #1
3 2542
weaknessforcats
9,208 Recognized Expert Moderator Expert
Use the Hollywood Principle (don't call us, we'll call you).

In this case, declare a private virtual function in the base class that is overridden by the derived classs.

Then when you create a derived object and access it as a base object you can call the base class virtual function which is really a call to the derived class virtual function giving you access to the derived data members.

This is part of the design pattern called Template Method.
Aug 27 '07 #2
jimhce
4 New Member
But the main problem here is that the non-static member function cannot be used by the pthread_create. I have to use a static member in the child class to allow the pthread_create to accept the function. The static cannot be used together with the virtual keyword. If I could find a way to allow pthread_create to accept a non-static member function, all the problem would resolved.

Thank you.

Jim
Aug 27 '07 #3
RRick
463 Recognized Expert Contributor
As for pthread, since its a C API, it won't be able to interpret or understand a non-static method from the class.

To get your class called by pthread you need to supply pthread_create with a static callback method from the class and pass the C++ object as the void * data. Pthread will call your static callback method with the object data. The static method reinterprets the void * data as the object and then you can call any method in the object.
Aug 27 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2030
by: David Chandler | last post by:
Let me try to describe the situation as clearly as I can. In namespace XXX I have a class PARENT with the following public functions: virtual void foo( const char* stringBuf ); // set function for foo virtual char* foo() const; // get function for foo both of these functions have implementations in the PARENT.cpp file.
12
1827
by: Joe | last post by:
Hi, Can I pass a "generic" class pointer as an argument to a function? For instance say classA and B are both derived from Z. { int iType =1;
2
3599
by: raxitsheth | last post by:
Hello All... I am using Posix Thread. class Parent { public: virtual void* func(void *)=0;
9
2087
by: jon wayne | last post by:
OK! I had this nagging doubt Consider (without worrying abt access specifiers) class Kid : public Parent{...}; Parent::someFunc() { Kid k; }
10
2512
by: Sean Dockery | last post by:
I have the following HTML file that I've been using for testing... <html> <head> <script type="text/javascript"> <!-- function handleWindowLoad() { var items = ; for (var i = 0; i < 11; i++) { items = "item" + (i + 1);
6
4730
by: kath | last post by:
hi everyone......... I have a task, I have fragmented the task into subtask. I have planned to create a class to each subclass and one parent class to handle the sub tasks. Each subclass are saved in seperate file and all my subclasses and parent class are placed in the same folder. The parent class is subclass of wx.Frame, and subclasses are subclass of wx.Panel. Each subclass will create a GUI for each task..
12
11112
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
9
2638
by: pereges | last post by:
Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the electromagnetic rays from the source, as they hit the object.The object is triangulated. The rays can undergo multiple reflections, diffractions etc of the same object i.e. a ray hits a surface of the object, undergoes reflection resulting in a reflected ray which can again hit a surface, corner or edge...
1
6474
by: Beamor | last post by:
function art_menu_xml_parcer($content, $showSubMenus) { $doc = new DOMDocument(); $doc->loadXML($content);//this is the line in question $parent = $doc->documentElement; $elements = $parent->childNodes; need help. my site worked fine on my localhost but when i uploaded it to my live server i keep getting this error need help to recode line to match my hosting server. im using php 5 i have attached the common_method file
0
9669
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
9515
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
10426
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
9029
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
7537
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
6776
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
5558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3713
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.