Problem with callbacks with arguments

Discussion in 'C++' started by johny smith, May 24, 2004.

  1. johny smith

    johny smith Guest

    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;

    }
     
    johny smith, May 24, 2004
    #1
    1. Advertising

  2. * "johny smith" <> 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?
     
    Alf P. Steinbach, May 24, 2004
    #2
    1. Advertising

  3. johny smith

    Luther Baker Guest

    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_mfc/callbacks/comments.php/c4123/?thread=49538

    -Luther
     
    Luther Baker, May 24, 2004
    #3
  4. johny smith

    DaKoadMunky Guest

    >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
     
    DaKoadMunky, May 25, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward Diener
    Replies:
    14
    Views:
    5,126
    Josiah Carlson
    Apr 6, 2004
  2. Neo
    Replies:
    10
    Views:
    705
    sushant
    Jan 20, 2005
  3. tutmann
    Replies:
    4
    Views:
    463
  4. jmborr
    Replies:
    1
    Views:
    459
    Stargaming
    Nov 3, 2007
  5. Replies:
    3
    Views:
    866
Loading...

Share This Page