c++ function pointer

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

    c++ function pointer

    Hi,

    I would like implement a class with a method Draw(),
    ..I would like that method Draw() change with another method when the
    state of class change.
    I think to use function pointer to do it, but I don't know how use this
    in a class?
    Can someone do a example?

  • Victor Bazarov

    #2
    Re: c++ function pointer

    Marco wrote:[color=blue]
    > I would like implement a class with a method Draw(),[/color]

    class a_class {
    void Draw();
    };
    [color=blue]
    > .I would like that method Draw() change with another method when the
    > state of class change.[/color]

    What does it mean "method Draw() change"? How can a method change?
    [color=blue]
    > I think to use function pointer to do it, but I don't know how use this
    > in a class?[/color]

    I don't think there is a need of any pointer. Just implement your 'Draw'
    so that it checks the "state of class" and behaves accordingly:

    void a_class::Draw() {
    if (state_didnt_ch ange) // whatever that means
    ; // do something here
    else // the state has changed
    ; // do something different
    }
    [color=blue]
    > Can someone do a example?[/color]

    I can do a example alright. Just give me the example and I'll do it.

    V

    Comment

    • n2xssvv g02gfr12930

      #3
      Re: c++ function pointer

      Marco wrote:[color=blue]
      > Hi,
      >
      > I would like implement a class with a method Draw(),
      > .I would like that method Draw() change with another method when the
      > state of class change.
      > I think to use function pointer to do it, but I don't know how use this
      > in a class?
      > Can someone do a example?
      >[/color]
      Marco,

      Why not create a base class with the Draw() method being declared as
      virtual and use a pointer to the base class. The actual Draw() function
      can then be defined in descendant classes, (see below).

      class Shape
      {
      public:
      virtual void Draw(void);

      Comment

      • osmium

        #4
        Re: c++ function pointer

        "Marco" writes:
        [color=blue]
        > I would like implement a class with a method Draw(),
        > .I would like that method Draw() change with another method when the
        > state of class change.
        > I think to use function pointer to do it, but I don't know how use this
        > in a class?
        > Can someone do a example?[/color]

        Do you know about polymorphism in C++? Perhaps that will help you do what
        you want. If you want to stick with the function pointer approach, here is
        a sample of the rather nasty syntax. But note that the function pointer is
        in main and not in the class. But there is a way to get access to the state
        variable should you wish to. I can show you how to get it too, but first I
        think you should knowingly reject the polymorphic approach.

        #include <iostream>

        class C
        {
        public:
        void draw();
        private:
        int state; // igonred in this test program
        };
        //--------------
        void C::draw()
        {
        std::cout << "draw called\n";
        }
        //=============== ===
        int main()
        {
        C object;
        typedef void( C::*PMF) (); // for clarity
        PMF pmf; // pointer to member function
        pmf = &C::draw; // select desired member function
        (object.*pmf) (); // call it

        std::cin.get();
        }


        Comment

        • Alf P. Steinbach

          #5
          Re: c++ function pointer

          * Marco:[color=blue]
          >
          > I would like implement a class with a method Draw(),
          > .I would like that method Draw() change with another method when the
          > state of class change.
          > I think to use function pointer to do it, but I don't know how use this
          > in a class?
          > Can someone do a example?[/color]

          Use a pointer to an instance of a class with a virtual member function.


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

          Comment

          • HappyHippy

            #6
            Re: c++ function pointer

            Marco wrote:[color=blue]
            > Hi,
            >
            > I would like implement a class with a method Draw(),
            > .I would like that method Draw() change with another method when the
            > state of class change.
            > I think to use function pointer to do it, but I don't know how use this
            > in a class?
            > Can someone do a example?
            >[/color]

            You can do something like that:

            #include <iostream>


            class CMYClass
            {
            public:
            enum E_STATE
            {
            E_ST1,
            E_ST2,
            E_ST3
            };
            CMYClass():
            m_pDrawFnc(&CMY Class::draw_S1)
            {
            }

            void draw()
            {
            (this->*m_pDrawFnc)() ;
            }

            void changeState(E_S TATE eState)
            {
            switch(eState)
            {
            case E_ST1:
            m_pDrawFnc = &CMYClass::draw _S1;
            return;

            case E_ST2:
            m_pDrawFnc = &CMYClass::draw _S2;
            return;

            case E_ST3:
            m_pDrawFnc = &CMYClass::draw _S3;
            return;

            default:
            //invalid argument!
            return;
            }
            }

            private:
            void draw_S1()
            {
            std::cout<<"dra w_S1"<<std::end l;
            }
            void draw_S2()
            {
            std::cout<<"dra w_S2"<<std::end l;
            }
            void draw_S3()
            {
            std::cout<<"dra w_S3"<<std::end l;
            }

            typedef void (CMYClass::*dra w_fnc)();

            E_STATE m_eState;
            draw_fnc m_pDrawFnc;
            };

            int main()
            {
            CMYClass obj;
            obj.draw();
            obj.changeState (CMYClass::E_ST 2);
            obj.draw();
            obj.changeState (CMYClass::E_ST 3);
            obj.draw();

            return 0;
            }

            Comment

            • osmium

              #7
              Re: c++ function pointer

              "Marco" writes:
              [color=blue]
              > I would like implement a class with a method Draw(),
              > .I would like that method Draw() change with another method when the
              > state of class change.
              > I think to use function pointer to do it, but I don't know how use this
              > in a class?
              > Can someone do a example?[/color]

              I think I was seduced by your talk of a function pointer in my earlier post.
              As I read your post literally, and ignoring your speculation, it seems you
              simply want something like this.

              #include <iostream>

              class C
              {
              public:
              C() {state = 0;}
              void draw();
              void change_state() { state = 1;}
              void draw1() {std::cout << "draw1 called\n";}
              void draw2() {std::cout << "draw2 called\n";}
              private:
              int state;
              };
              //---------------------
              void C::draw()
              {
              if (state == 0)
              draw1();
              else
              draw2();
              }
              //=============== ==
              int main()
              {
              C c;
              c.draw1();
              c.change_state( );
              c.draw();

              std::cin.get();
              }


              Comment

              • Marco

                #8
                Re: c++ function pointer

                Thanks it's right what I need.

                Comment

                • osmium

                  #9
                  Re: c++ function pointer

                  "osmium" <r124c4u102@com cast.net> wrote in message
                  news:3sfechFng0 usU1@individual .net...
                  [color=blue]
                  > C c;
                  > c.draw1();[/color]

                  Oops! Shoule be c.draw();
                  [color=blue]
                  > c.change_state( );
                  > c.draw();[/color]


                  Comment

                  • Andrej Hristoliubov

                    #10
                    Re: c++ function pointer

                    ?[color=blue]
                    >
                    > I can do a example alright. Just give me the example and I'll do it.
                    >
                    > V[/color]
                    Vitya is right again!!!

                    Do listen to Victor, I neither had seen this in production not
                    Victor's code. And I know Victor for quite some time now (Vitya is it
                    25 or 30 years in total is the duration of our acquaintance and
                    bestfriendship? ).


                    hi to babulya....

                    Comment

                    • gary

                      #11
                      Re: c++ function pointer


                      On Fri, 28 Oct 2005 15:48:27 -0400
                      HappyHippy <karenman@mail. ru> wrote:
                      [color=blue]
                      > Marco wrote:[color=green]
                      > > Hi,
                      > >
                      > > I would like implement a class with a method Draw(),
                      > > .I would like that method Draw() change with another method when the
                      > > state of class change.
                      > > I think to use function pointer to do it, but I don't know how use this
                      > > in a class?
                      > > Can someone do a example?
                      > >[/color]
                      >
                      > You can do something like that:
                      >
                      > #include <iostream>
                      >
                      >
                      > class CMYClass
                      > {
                      > public:
                      > enum E_STATE
                      > {
                      > E_ST1,
                      > E_ST2,
                      > E_ST3
                      > };
                      > CMYClass():
                      > m_pDrawFnc(&CMY Class::draw_S1)
                      > {
                      > }
                      >
                      > void draw()
                      > {
                      > (this->*m_pDrawFnc)() ;
                      > }
                      >
                      > void changeState(E_S TATE eState)
                      > {
                      > switch(eState)
                      > {
                      > case E_ST1:
                      > m_pDrawFnc = &CMYClass::draw _S1;
                      > return;
                      >
                      > case E_ST2:
                      > m_pDrawFnc = &CMYClass::draw _S2;
                      > return;
                      >
                      > case E_ST3:
                      > m_pDrawFnc = &CMYClass::draw _S3;
                      > return;
                      >
                      > default:
                      > //invalid argument!
                      > return;
                      > }
                      > }
                      >
                      > private:
                      > void draw_S1()
                      > {
                      > std::cout<<"dra w_S1"<<std::end l;
                      > }
                      > void draw_S2()
                      > {
                      > std::cout<<"dra w_S2"<<std::end l;
                      > }
                      > void draw_S3()
                      > {
                      > std::cout<<"dra w_S3"<<std::end l;
                      > }
                      >
                      > typedef void (CMYClass::*dra w_fnc)();
                      >
                      > E_STATE m_eState;
                      > draw_fnc m_pDrawFnc;
                      > };
                      >
                      > int main()
                      > {
                      > CMYClass obj;
                      > obj.draw();
                      > obj.changeState (CMYClass::E_ST 2);
                      > obj.draw();
                      > obj.changeState (CMYClass::E_ST 3);
                      > obj.draw();
                      >
                      > return 0;
                      > }[/color]

                      I think the resolution is still not adequate.
                      If the collection of states you need is greater than E_STATE, what should you do?
                      I think using a integer variable or array is simple, every bit of the variable represents a state or simplely the value of the variable represents states

                      Comment

                      Working...