By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,132 Members | 1,967 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,132 IT Pros & Developers. It's quick & easy.

Problem with callbacks with arguments

P: n/a
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;

}
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* "johny smith" <pr**************@charter.net> schriebt:
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.
car2.check_engine();

return 0;

}


--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2

P: n/a
johny smith wrote:
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.


How about using a functor?

http://www.codeguru.com/Cpp/Cpp/cpp_.../?thread=49538

-Luther
Jul 22 '05 #3

P: n/a
>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
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.