Solved

NewBie question : call back function.

Posted on 1998-07-11
3
239 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

685 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