J
johny smith
I am having trouble with a basic concept and need some help.
What I want to do is upon construction of an object I want to pass in a
callback function name with an argument.
The class then stores that callback function.
At an appropriate time, the callback function is called with the argument.
I created an example that illustrates this, but it does not work and don't
really understand why.
Please help.
Thanks in advance for any assistance in this matter.
#include <iostream>
// this operation passes in the failure type.
void engine_fail( int failure_type )
{
// print out the failure type passed in.
std::cout << "engine failure type is " << failure_type << std::endl;
}
class car {
public:
void (*func)( int ); // class variable to store the call back function.
car( void (*engine_fail)( int ) )
{
// the constructor stores off the passed in function
// for later use in another function.
//
func = enginer_fail;
}
void check_engine()
{
int error_code = 1;
// now call back the call back function with the passed in argument.
// I am assuming in this example that an error has occured and the
// callback function must be called.
func( error_code );
}
};
int main()
{
// create a car object and pass in the funtion to call back
// if there is a failure.
//
car car2( engine_failure_function );
car2.check_engine();
return 0;
}
What I want to do is upon construction of an object I want to pass in a
callback function name with an argument.
The class then stores that callback function.
At an appropriate time, the callback function is called with the argument.
I created an example that illustrates this, but it does not work and don't
really understand why.
Please help.
Thanks in advance for any assistance in this matter.
#include <iostream>
// this operation passes in the failure type.
void engine_fail( int failure_type )
{
// print out the failure type passed in.
std::cout << "engine failure type is " << failure_type << std::endl;
}
class car {
public:
void (*func)( int ); // class variable to store the call back function.
car( void (*engine_fail)( int ) )
{
// the constructor stores off the passed in function
// for later use in another function.
//
func = enginer_fail;
}
void check_engine()
{
int error_code = 1;
// now call back the call back function with the passed in argument.
// I am assuming in this example that an error has occured and the
// callback function must be called.
func( error_code );
}
};
int main()
{
// create a car object and pass in the funtion to call back
// if there is a failure.
//
car car2( engine_failure_function );
car2.check_engine();
return 0;
}