Callback to a non-static method?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Levin Alexander

    Callback to a non-static method?

    Hi,

    (I hope that this is not a FAQ)

    I'm trying to make a timer call a method of an object.

    void MyClass::myCall back(int par)
    {cout << "Callback Called" << std::endl;}

    void MyClass::addTim er()
    {
    signal(SIGALRM, myCallback);
    // ...
    }

    This does not work. The Error is
    "argument of type 'void (MyClass::)(int )' does not match 'void (*)(int)'"

    Why is that not working? Is there a workaround?

    Thanks,
    Levin
  • Alf P. Steinbach

    #2
    Re: Callback to a non-static method?

    * "Levin Alexander" <levin@grundeis .net> schriebt:[color=blue]
    >
    > I'm trying to make a timer call a method of an object.
    >
    > void MyClass::myCall back(int par)
    > {cout << "Callback Called" << std::endl;}
    >
    > void MyClass::addTim er()
    > {
    > signal(SIGALRM, myCallback);
    > // ...
    > }
    >
    > This does not work. The Error is
    > "argument of type 'void (MyClass::)(int )' does not match 'void (*)(int)'"[/color]

    Be glad you got a compilation error, because mixing C signals and C++
    object-oriented code is simply not supported, and leads to UB.

    So the technical error prevented you from making a much more serious
    and much more difficult to pin down error.

    Many others will probably explain how to do The Technical Thing (TM)
    to get the code to compile (thus introducing the Grave Error (TM)).

    --
    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?

    Comment

    • Alf P. Steinbach

      #3
      Re: Callback to a non-static method?

      * "Levin Alexander" <levin@grundeis .net> schriebt:[color=blue]
      >
      > I'm trying to make a timer call a method of an object.
      >
      > void MyClass::myCall back(int par)
      > {cout << "Callback Called" << std::endl;}
      >
      > void MyClass::addTim er()
      > {
      > signal(SIGALRM, myCallback);
      > // ...
      > }
      >
      > This does not work. The Error is
      > "argument of type 'void (MyClass::)(int )' does not match 'void (*)(int)'"[/color]

      Be glad you got a compilation error, because mixing C signals and C++
      object-oriented code is simply not supported, and leads to UB.

      So the technical error prevented you from making a much more serious
      and much more difficult to pin down error.

      Many others will probably explain how to do The Technical Thing (TM)
      to get the code to compile (thus introducing the Grave Error (TM)).

      --
      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?

      Comment

      • John Harrison

        #4
        Re: Callback to a non-static method?


        "Levin Alexander" <levin@grundeis .net> wrote in message
        news:opr57vx2ez 5gefrg@news.rwt h-aachen.de...[color=blue]
        > Hi,
        >
        > (I hope that this is not a FAQ)
        >[/color]

        Err it is.



        john


        Comment

        • John Harrison

          #5
          Re: Callback to a non-static method?


          "Levin Alexander" <levin@grundeis .net> wrote in message
          news:opr57vx2ez 5gefrg@news.rwt h-aachen.de...[color=blue]
          > Hi,
          >
          > (I hope that this is not a FAQ)
          >[/color]

          Err it is.



          john


          Comment

          • Levin Alexander

            #6
            Re: Callback to a non-static method?

            Alf P. Steinbach <alfps@start.no > wrote:
            [color=blue][color=green]
            >> void MyClass::myCall back(int par)
            >> {cout << "Callback Called" << std::endl;}
            >> void MyClass::addTim er()
            >> {
            >> signal(SIGALRM, myCallback);
            >> // ...
            >> }[/color]
            > Be glad you got a compilation error, because mixing C signals and C++
            > object-oriented code is simply not supported, and leads to UB.[/color]

            (I sense the need to buy a decent book.)

            Is there a "pure" C++ way to solve that problem?

            Thanks,
            Levin

            Comment

            • Levin Alexander

              #7
              Re: Callback to a non-static method?

              Alf P. Steinbach <alfps@start.no > wrote:
              [color=blue][color=green]
              >> void MyClass::myCall back(int par)
              >> {cout << "Callback Called" << std::endl;}
              >> void MyClass::addTim er()
              >> {
              >> signal(SIGALRM, myCallback);
              >> // ...
              >> }[/color]
              > Be glad you got a compilation error, because mixing C signals and C++
              > object-oriented code is simply not supported, and leads to UB.[/color]

              (I sense the need to buy a decent book.)

              Is there a "pure" C++ way to solve that problem?

              Thanks,
              Levin

              Comment

              • Alf P. Steinbach

                #8
                Re: Callback to a non-static method?

                * "Levin Alexander" <levin@grundeis .net> schriebt:[color=blue]
                > Alf P. Steinbach <alfps@start.no > wrote:
                >[color=green][color=darkred]
                > >> void MyClass::myCall back(int par)
                > >> {cout << "Callback Called" << std::endl;}
                > >> void MyClass::addTim er()
                > >> {
                > >> signal(SIGALRM, myCallback);
                > >> // ...
                > >> }[/color]
                > > Be glad you got a compilation error, because mixing C signals and C++
                > > object-oriented code is simply not supported, and leads to UB.[/color]
                >
                > (I sense the need to buy a decent book.)
                >
                > Is there a "pure" C++ way to solve that problem?[/color]

                Not "pure", but by using platform-specific functionality you can implement
                timers that are, for example, exception safe.

                --
                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?

                Comment

                • Alf P. Steinbach

                  #9
                  Re: Callback to a non-static method?

                  * "Levin Alexander" <levin@grundeis .net> schriebt:[color=blue]
                  > Alf P. Steinbach <alfps@start.no > wrote:
                  >[color=green][color=darkred]
                  > >> void MyClass::myCall back(int par)
                  > >> {cout << "Callback Called" << std::endl;}
                  > >> void MyClass::addTim er()
                  > >> {
                  > >> signal(SIGALRM, myCallback);
                  > >> // ...
                  > >> }[/color]
                  > > Be glad you got a compilation error, because mixing C signals and C++
                  > > object-oriented code is simply not supported, and leads to UB.[/color]
                  >
                  > (I sense the need to buy a decent book.)
                  >
                  > Is there a "pure" C++ way to solve that problem?[/color]

                  Not "pure", but by using platform-specific functionality you can implement
                  timers that are, for example, exception safe.

                  --
                  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?

                  Comment

                  • Allan Bruce

                    #10
                    Re: Callback to a non-static method?


                    "Levin Alexander" <levin@grundeis .net> wrote in message
                    news:opr57vx2ez 5gefrg@news.rwt h-aachen.de...[color=blue]
                    > Hi,
                    >
                    > (I hope that this is not a FAQ)
                    >
                    > I'm trying to make a timer call a method of an object.
                    >
                    > void MyClass::myCall back(int par)
                    > {cout << "Callback Called" << std::endl;}
                    >
                    > void MyClass::addTim er()
                    > {
                    > signal(SIGALRM, myCallback);
                    > // ...
                    > }
                    >
                    > This does not work. The Error is
                    > "argument of type 'void (MyClass::)(int )' does not match 'void (*)(int)'"
                    >
                    > Why is that not working? Is there a workaround?
                    >
                    > Thanks,
                    > Levin[/color]

                    You could have one static member which takes a pointer to the type of class
                    it belongs, then you could pass it the 'this' pointer.

                    e.g.

                    void MyClass::Callba ck(MyClass *xiThis, ...)
                    {
                    xiThis->NonStaticCallb ack(...);
                    }

                    HTH
                    Allan


                    Comment

                    • Allan Bruce

                      #11
                      Re: Callback to a non-static method?


                      "Levin Alexander" <levin@grundeis .net> wrote in message
                      news:opr57vx2ez 5gefrg@news.rwt h-aachen.de...[color=blue]
                      > Hi,
                      >
                      > (I hope that this is not a FAQ)
                      >
                      > I'm trying to make a timer call a method of an object.
                      >
                      > void MyClass::myCall back(int par)
                      > {cout << "Callback Called" << std::endl;}
                      >
                      > void MyClass::addTim er()
                      > {
                      > signal(SIGALRM, myCallback);
                      > // ...
                      > }
                      >
                      > This does not work. The Error is
                      > "argument of type 'void (MyClass::)(int )' does not match 'void (*)(int)'"
                      >
                      > Why is that not working? Is there a workaround?
                      >
                      > Thanks,
                      > Levin[/color]

                      You could have one static member which takes a pointer to the type of class
                      it belongs, then you could pass it the 'this' pointer.

                      e.g.

                      void MyClass::Callba ck(MyClass *xiThis, ...)
                      {
                      xiThis->NonStaticCallb ack(...);
                      }

                      HTH
                      Allan


                      Comment

                      • Alf P. Steinbach

                        #12
                        Re: Callback to a non-static method?

                        * "Allan Bruce" <allanmb@TAKEAW AYf2s.com> schriebt:[color=blue]
                        >
                        > You could have one static member which takes a pointer to the type of class
                        > it belongs, then you could pass it the 'this' pointer.
                        >
                        > e.g.
                        >
                        > void MyClass::Callba ck(MyClass *xiThis, ...)
                        > {
                        > xiThis->NonStaticCallb ack(...);
                        > }[/color]

                        Although the above will work with all relevant compilers (AFAIK), it's
                        officially ungood. A C callback needs to be 'extern "C"'. Hence
                        it needs to be a namespace scope function.

                        --
                        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?

                        Comment

                        • Alf P. Steinbach

                          #13
                          Re: Callback to a non-static method?

                          * "Allan Bruce" <allanmb@TAKEAW AYf2s.com> schriebt:[color=blue]
                          >
                          > You could have one static member which takes a pointer to the type of class
                          > it belongs, then you could pass it the 'this' pointer.
                          >
                          > e.g.
                          >
                          > void MyClass::Callba ck(MyClass *xiThis, ...)
                          > {
                          > xiThis->NonStaticCallb ack(...);
                          > }[/color]

                          Although the above will work with all relevant compilers (AFAIK), it's
                          officially ungood. A C callback needs to be 'extern "C"'. Hence
                          it needs to be a namespace scope function.

                          --
                          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?

                          Comment

                          • DaKoadMunky

                            #14
                            Re: Callback to a non-static method?

                            >Many others will probably explain how to do The Technical Thing (TM)[color=blue]
                            >to get the code to compile (thus introducing the Grave Error (TM)).[/color]

                            Could you explain further please?

                            What is The Technical Thing?

                            What is The Grave Error?


                            Brian F. Seaberg
                            Naperville, Illinois
                            Delray Beach, Florida

                            Comment

                            • DaKoadMunky

                              #15
                              Re: Callback to a non-static method?

                              >Many others will probably explain how to do The Technical Thing (TM)[color=blue]
                              >to get the code to compile (thus introducing the Grave Error (TM)).[/color]

                              Could you explain further please?

                              What is The Technical Thing?

                              What is The Grave Error?


                              Brian F. Seaberg
                              Naperville, Illinois
                              Delray Beach, Florida

                              Comment

                              Working...