Solved

NewBie question : call back function.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ array loading from DB inheriting previous data when empty 5 59
why "." vs "->" 23 119
how to convert c++ code to Android App 3 80
Find Visual Studio Tools 2 92
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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 clear a vector as well as how to detect empty vectors in C++.

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now