Solved

NewBie question : call back function.

Posted on 1998-07-11
3
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

738 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