Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem with callbacks with arguments

Reply
Thread Tools

Problem with callbacks with arguments

 
 
johny smith
Guest
Posts: n/a
 
      05-24-2004
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;

}


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-24-2004
* "johny smith" <(E-Mail Removed)> 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?
 
Reply With Quote
 
 
 
 
Luther Baker
Guest
Posts: n/a
 
      05-24-2004
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
 
Reply With Quote
 
DaKoadMunky
Guest
Posts: n/a
 
      05-25-2004
>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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Call again a variadic function (... variable number of arguments)with same arguments that its variadic wrapper moreau.steve@gmail.com C Programming 3 12-31-2008 07:13 AM
how to pass a function name and its arguments inside the arguments of other function? jmborr Python 1 11-03-2007 08:20 AM
function default arguments from other arguments tutmann C++ 4 10-17-2006 08:00 PM
function call with arguments which takes no arguments Neo C Programming 10 01-20-2005 06:31 AM
Difference between default arguments and keyword arguments Edward Diener Python 14 04-05-2004 11:26 PM



Advertisments