member function pointer and STL

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

    member function pointer and STL

    hallo,
    the following program works OK:
    #include <stdio.h>

    struct A {
    bool fun(int a, int b) {
    return a == b;
    }

    bool (A::*ptr)(int a, int b);
    A() {
    ptr = &A::fun;
    }

    bool fun_ptr(int a, int b) {
    return (A::ptr)(a, b);
    }
    };

    main() {
    A a;
    printf ("%d\n", a.fun_ptr(4,4) );
    printf ("%d\n", a.fun_ptr(4,3) );
    }


    But, after changing into STL class:

    #include <stdio.h>

    template <class T>
    struct A {
    bool fun(T a, T b) {
    return a == b;
    }

    bool (A::*ptr)(T a, T b);
    A() {
    ptr = &A::fun;
    }

    bool fun_ptr(T a, T b) {
    return (A::ptr)(a, b); // this is line 15
    }
    };

    main() {
    A<int> a;
    printf ("%d\n", a.fun_ptr(4,4) );
    printf ("%d\n", a.fun_ptr(4,4) );
    }

    during compilation the following error occures:

    /home/mkwap/BGP-cvs/ccc/p11.cpp: In method `bool
    A<int>::fun_ptr <int>(int, int)':
    /home/mkwap/BGP-cvs/ccc/p11.cpp:21: instantiated from here
    /home/mkwap/BGP-cvs/ccc/p11.cpp:15: pointer-to-member function
    A<int>::ptr cannot be called
    /home/mkwap/BGP-cvs/ccc/p11.cpp:15: without an object; consider
    using .* or ->*

    Do You know what is the problem and what to do to make second
    program run

    Greetings
    Maciej
  • Victor Bazarov

    #2
    Re: member function pointer and STL

    "Maciej Kwapulinski" <pikpok@julia.u niv.gda.pl> wrote...[color=blue]
    > hallo,
    > the following program works OK:[/color]

    "works OK" is not enough to be correct. It contains at least
    two errors. See below.
    [color=blue]
    > #include <stdio.h>
    >
    > struct A {
    > bool fun(int a, int b) {
    > return a == b;
    > }
    >
    > bool (A::*ptr)(int a, int b);
    > A() {
    > ptr = &A::fun;
    > }
    >
    > bool fun_ptr(int a, int b) {
    > return (A::ptr)(a, b);[/color]

    This is incorrect. Needs to be

    return (this->*ptr)(a, b);
    [color=blue]
    > }
    > };
    >
    > main() {[/color]

    This is incorrect. Needs to be

    int main() {
    [color=blue]
    > A a;
    > printf ("%d\n", a.fun_ptr(4,4) );
    > printf ("%d\n", a.fun_ptr(4,3) );
    > }
    >
    >
    > But, after changing into STL class:[/color]

    What you change your class into is not an "STL class".
    It's a class template.
    [color=blue]
    >
    > #include <stdio.h>
    >
    > template <class T>
    > struct A {
    > bool fun(T a, T b) {
    > return a == b;
    > }
    >
    > bool (A::*ptr)(T a, T b);
    > A() {
    > ptr = &A::fun;
    > }
    >
    > bool fun_ptr(T a, T b) {
    > return (A::ptr)(a, b); // this is line 15[/color]

    return (this->*ptr)(a, b); // this is line 15
    [color=blue]
    > }
    > };
    >
    > main() {[/color]

    int main() {

    (as you can see both changes are _precisely_ the same as in the
    non-template code)
    [color=blue]
    > A<int> a;
    > printf ("%d\n", a.fun_ptr(4,4) );
    > printf ("%d\n", a.fun_ptr(4,4) );
    > }
    >
    > during compilation the following error occures:
    >
    > /home/mkwap/BGP-cvs/ccc/p11.cpp: In method `bool
    > A<int>::fun_ptr <int>(int, int)':
    > /home/mkwap/BGP-cvs/ccc/p11.cpp:21: instantiated from here
    > /home/mkwap/BGP-cvs/ccc/p11.cpp:15: pointer-to-member function
    > A<int>::ptr cannot be called
    > /home/mkwap/BGP-cvs/ccc/p11.cpp:15: without an object; consider
    > using .* or ->*
    >
    > Do You know what is the problem and what to do to make second
    > program run[/color]

    It will compile (and hopefully run) just fine if you make the
    changes I mentioned.

    Victor


    Comment

    • Rob Williscroft

      #3
      Re: member function pointer and STL

      Maciej Kwapulinski wrote in news:slrnbfh4oi .bkg.pikpok@jul ia.univ.gda.pl:
      [color=blue]
      > hallo,
      > the following program works OK:[/color]

      It shouldn't (AFAICT).

      Try compiling with all warning/error's on,
      for example -Wall -ansi -pedantic with g++.

      If that doesn't work get a better compiler if you can.
      [color=blue]
      > #include <stdio.h>
      >
      > struct A {
      > bool fun(int a, int b) {
      > return a == b;
      > }
      >
      > bool (A::*ptr)(int a, int b);
      > A() {
      > ptr = &A::fun;
      > }
      >
      > bool fun_ptr(int a, int b) {
      > return (A::ptr)(a, b);[/color]

      return (this->*ptr)(a, b);

      This is guess. But I suspect you may think that the 'A::' you
      wrote above is telling the compiler which object to call the ptr
      on. If so it isn't, it's actually telling the compiler to use the
      'ptr' that is in the scope of A and since fun_ptr() is a member
      of A this is the default, IOW it does nothing in this context.

      [color=blue]
      > }
      > };
      >
      > main() {
      > A a;
      > printf ("%d\n", a.fun_ptr(4,4) );
      > printf ("%d\n", a.fun_ptr(4,3) );
      > }
      >
      >
      > But, after changing into STL class:[/color]

      STL is a TLA that expands to [S]tandard [T]emplate [L]ibrary.

      What you have here is a class template.
      [color=blue]
      >
      > #include <stdio.h>
      >
      > template <class T>
      > struct A {
      > bool fun(T a, T b) {
      > return a == b;
      > }
      >
      > bool (A::*ptr)(T a, T b);
      > A() {
      > ptr = &A::fun;
      > }
      >
      > bool fun_ptr(T a, T b) {
      > return (A::ptr)(a, b); // this is line 15[/color]

      same as before:

      return (this->*ptr)(a, b);
      [color=blue]
      > }
      > };
      >
      > main() {
      > A<int> a;
      > printf ("%d\n", a.fun_ptr(4,4) );
      > printf ("%d\n", a.fun_ptr(4,4) );
      > }
      >
      > during compilation the following error occures:
      >
      >/home/mkwap/BGP-cvs/ccc/p11.cpp: In method `bool
      >A<int>::fun_pt r<int>(int, int)':
      >/home/mkwap/BGP-cvs/ccc/p11.cpp:21: instantiated from here
      >/home/mkwap/BGP-cvs/ccc/p11.cpp:15: pointer-to-member function
      >A<int>::ptr cannot be called
      >/home/mkwap/BGP-cvs/ccc/p11.cpp:15: without an object; consider
      > using .* or ->*
      >[/color]

      This error message actually tells you how to fix the error!
      Which is nice, A shame you didn't get it for the first programme.
      [color=blue]
      > Do You know what is the problem and what to do to make second
      > program run
      >[/color]

      HTH

      Rob.
      --

      Comment

      Working...