Problem with callbacks with arguments

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;

}
 
A

Alf P. Steinbach

* "johny smith said:
I am having trouble with a basic concept and need some help.

It seems the trouble is with writing code that compiles, not concepts.


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.

Terminology: it's not a class variable, it's a member variable.

Naming: "func" is a very bad name because... Why is it such a bad name?


car( void (*engine_fail)( int ) )
{
// the constructor stores off the passed in function
// for later use in another function.
//
func = enginer_fail;

Typo; this should not compile.
}


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 );

Thinko: 'engine_failure_function' is nowhere defined.
 
D

DaKoadMunky

Terminology: it's not a class variable, it's a member variable.

I tend to refer to non-static variables as "instance variables" and static
variables as "class variables."

If someone asked me what a "member variable" was I would say that it is a
variable that is declared in a class definition. I wouldn't consider storage
class.

Is your usage of the term "member variable" common within the C++ community?
Am I just being polluted by the other OOP language I am trying to learn?

Regards,
Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top