Solved

NewBie question : call back function.

Posted on 1998-07-11
3
227 Views
Last Modified: 2010-04-10
From the example of Booch's book :
Object Oriented Analysis and Design with Application (p.45,46)
There are two similar classes :
e.g. 1 :
typedef float temperature;
typedef unsigned int location;
class temperaturesensor{
public :
temperaturesensor(location);
~temperaturesensor();
void calibrate(temperature actual temperature);
temperature currenttemperature() const;
private :
.....
}


e.g. 2 :
class activetemperaturesensor{
public :
activetemperaturesensor(location,void (*f) (location, temperature));
~activetemperaturesensor();
void calibrate(temperature actualtemperature);
void establishsetpoint(temperature setpoint, temperature delta);
temperature currenttemperature() const ;
private :
....
}


The second one has a callback function and is so called a
more 'active' class.
(the first one is called a 'passive' one.)
Can someone explain it more detail?

many thanks.
rgds.
alanpong@hkstar.com
0
Comment
Question by:alanpong
  • 2
3 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 50 total points
ID: 1167579
>> activetemperaturesensor(location,void (*f) (location, temperature));

The constructor for the second class takes a pointer to a user-function of the form:
    void function_name(location, temperature)

The supplied function presumably does something in a way that the designer of the class could not anticipate, so it is left to the creator (user) of the class to provide.

The constructor of the "active" class either invokes the supplied function directly
    f(some_location, some_temp);
or saves the supplied pointer in a private member variable and then has some other member function invoke it.

Example of usage:

    // Prototype:
    void MyFunc(location, temperature);
   
    // ...

    // Object creation:
    const unsigned int MY_LOCATION = 17;
    activetemperaturesensor mySensor(MY_LOCATION, MyFunc);

    // Implementation:
    void MyFunc(location, temperature)
    {
        // Do something...
    }

OK?


0
 
LVL 1

Author Comment

by:alanpong
ID: 1167580
very happy!
0
 
LVL 11

Expert Comment

by:alexo
ID: 1167581
Have fun!
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Focus not getting shifted out of  editbox 2 63
C++ finding a sting in a char* string from a text file 3 112
Problem with SqlConnection 4 171
Why isn't object file created? 6 61
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question