473,386 Members | 1,706 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,386 software developers and data experts.

Pass function point from child class to parent class

4
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 2518
weaknessforcats
9,208 Expert Mod 8TB
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
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 Expert 256MB
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
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...
12
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
by: raxitsheth | last post by:
Hello All... I am using Posix Thread. class Parent { public: virtual void* func(void *)=0;
9
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
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++)...
6
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...
12
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....
9
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...
1
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 =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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...

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.