Solved

NewBie question : call back function.

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

757 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

18 Experts available now in Live!

Get 1:1 Help Now