pointer to function problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • blackrosezy@gmail.com

    pointer to function problem

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    int (*test)();
    int (*test2)();

    int aku(){

    static int i = 0;
    cout << "NO : " << i << endl;
    i++;
    }


    int main()
    {

    test = &aku;
    test2 = &aku;
    (*test)();
    (*test)();

    return 0;
    }


    The output is :
    NO : 0
    NO : 1

    My question is,how to make test and test2 independent,alt hough they
    refer to same function.
    That's mean the output is :
    NO : 0
    NO : 0

  • Jakob Bieling

    #2
    Re: pointer to function problem

    blackrosezy@gma il.com wrote:[color=blue]
    > #include <cstdlib>
    > #include <iostream>
    >
    > using namespace std;
    >
    > int (*test)();
    > int (*test2)();
    >
    > int aku(){
    >
    > static int i = 0;
    > cout << "NO : " << i << endl;
    > i++;
    > }
    >
    >
    > int main()
    > {
    >
    > test = &aku;
    > test2 = &aku;
    > (*test)();
    > (*test)();
    >
    > return 0;
    > }
    >
    >
    > The output is :
    > NO : 0
    > NO : 1
    >
    > My question is,how to make test and test2 independent,alt hough they
    > refer to same function.
    > That's mean the output is :
    > NO : 0
    > NO : 0[/color]

    You cannot. Unless you create a new function (this includes
    different template instanciations) , the static variable inside that
    function will have those 'static' semantics you are experiencing.

    hth
    --
    jb

    (reply address in rot13, unscramble first)


    Comment

    • Tomás

      #3
      Re: pointer to function problem

      posted:
      [color=blue]
      > #include <cstdlib>
      > #include <iostream>
      >
      > using namespace std;
      >
      > int (*test)();
      > int (*test2)();
      >
      > int aku(){
      >
      > static int i = 0;
      > cout << "NO : " << i << endl;
      > i++;
      > }
      >
      >
      > int main()
      > {
      >
      > test = &aku;
      > test2 = &aku;
      > (*test)();
      > (*test)();
      >
      > return 0;
      > }
      >
      >
      > The output is :
      > NO : 0
      > NO : 1
      >
      > My question is,how to make test and test2 independent,alt hough they
      > refer to same function.
      > That's mean the output is :
      > NO : 0
      > NO : 0[/color]

      One way comes to mind:


      #include <cstdlib>
      #include <iostream>

      using namespace std;

      int (*test)();
      extern char const funcpoint_name_ test[] = "test";

      int (*test2)();
      extern char const funcpoint_name_ test2[] = "test2";


      template<const char* name_of_pointer >
      int aku()
      {

      static int i = 0;
      cout << "NO : " << i << endl;
      ++i;
      }

      int main()
      {

      test = &aku<funcpoint_ name_test>;

      test2 = &aku<funcpoint_ name_test2>;

      (*test)();

      (*test2)();

      return 0;
      }


      Comment

      • Jerry Coffin

        #4
        Re: pointer to function problem

        In article <1142771353.247 507.231190
        @v46g2000cwv.go oglegroups.com> , blackrosezy@gma il.com
        says...

        [ ... ]
        [color=blue]
        > My question is,how to make test and test2 independent,alt hough they
        > refer to same function.
        > That's mean the output is :
        > NO : 0
        > NO : 0[/color]

        Using pointers to a function, you can't. You can,
        however, create a functor that does the job:

        struct aku {
        int i;
        aku() : i(0) {}

        void operator()() {
        std::cout << "NO: " << i << std::endl;
        ++i;
        }
        };

        Each object you create of this type has its own,
        independent counter. For example:

        int main() {
        aku test1;
        aku test2;

        test1();
        test2();
        test1();
        test2();
        return 0;
        }

        produces:

        NO: 0
        NO: 0
        NO: 1
        NO: 1

        as its output. I think that's what you wanted anyway.

        --
        Later,
        Jerry.

        The universe is a figment of its own imagination.

        Comment

        Working...