Pointers to member functions and default parameters

Discussion in 'C++' started by Dave, Apr 29, 2004.

  1. Dave

    Dave Guest

    Hello all,

    At the line marked "Problem here???" below, I get successful compilation on
    one platform and failure on another. What does the Standard say about this?
    Is this a correct program? May a member function be called through a
    pointer when some parameter default values are accepted (i.e. not all
    parameters are passed)?

    Thanks,
    Dave


    #include <iostream>

    using namespace std;

    class foo
    {
    public:
    int do_it(int = 10) {return 42;}
    };

    template <typename RET, typename OBJECT_TYPE, typename PTR_TO_MEM_FUN>
    RET call_it(OBJECT_TYPE &ds, PTR_TO_MEM_FUN pm)
    {
    return (ds.*pm)(); // Problem here???
    }

    int main()
    {
    foo bar;

    cout << call_it<int>(bar, &foo::do_it) << endl;
    }
    Dave, Apr 29, 2004
    #1
    1. Advertising

  2. Dave <> spoke thus:

    > #include <iostream>


    > using namespace std;


    > class foo
    > {
    > public:
    > int do_it(int = 10) {return 42;}
    > };


    > template <typename RET, typename OBJECT_TYPE, typename PTR_TO_MEM_FUN>
    > RET call_it(OBJECT_TYPE &ds, PTR_TO_MEM_FUN pm)
    > {
    > return (ds.*pm)(); // Problem here???

    ^^^^ I don't think this is legal :)

    > }


    > int main()
    > {
    > foo bar;


    > cout << call_it<int>(bar, &foo::do_it) << endl;
    > }


    (I'm not an authority; any of the below could be [really?] wrong!)

    Well, among other things, you didn't complete the template
    specification (I'm sure that's the wrong word...) - the template takes
    three arguments, and you only supplied one. Another thing is that
    template arguments should be object types, and a pointer to a member
    function doesn't sound like it belongs there.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Apr 30, 2004
    #2
    1. Advertising

  3. Dave

    Dave Guest

    "Christopher Benson-Manica" <> wrote in message
    news:c6tih5$p3u$...
    > Dave <> spoke thus:
    >
    > > #include <iostream>

    >
    > > using namespace std;

    >
    > > class foo
    > > {
    > > public:
    > > int do_it(int = 10) {return 42;}
    > > };

    >
    > > template <typename RET, typename OBJECT_TYPE, typename PTR_TO_MEM_FUN>
    > > RET call_it(OBJECT_TYPE &ds, PTR_TO_MEM_FUN pm)
    > > {
    > > return (ds.*pm)(); // Problem here???

    > ^^^^ I don't think this is legal :)
    >
    > > }

    >
    > > int main()
    > > {
    > > foo bar;

    >
    > > cout << call_it<int>(bar, &foo::do_it) << endl;
    > > }

    >
    > (I'm not an authority; any of the below could be [really?] wrong!)
    >
    > Well, among other things, you didn't complete the template
    > specification (I'm sure that's the wrong word...) - the template takes
    > three arguments, and you only supplied one. Another thing is that
    > template arguments should be object types, and a pointer to a member
    > function doesn't sound like it belongs there.
    >
    > --
    > Christopher Benson-Manica | I *should* know what I'm talking about - if I
    > ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


    The other two template parameters are deduced by the compiler.

    A template type parameter does not have to be of class type. It may be of a
    built-in type, an enumeration, a pointer-to-member, etc...
    Dave, Apr 30, 2004
    #3
    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. Jason
    Replies:
    2
    Views:
    492
    Jonathan Mcdougall
    May 13, 2006
  2. Hamish
    Replies:
    3
    Views:
    553
    Alf P. Steinbach
    Jan 25, 2008
  3. Hicham Mouline
    Replies:
    0
    Views:
    420
    Hicham Mouline
    Apr 23, 2009
  4. Hicham Mouline
    Replies:
    1
    Views:
    404
    Michael DOUBEZ
    Apr 24, 2009
  5. paul
    Replies:
    8
    Views:
    699
    Alf P. Steinbach
    Apr 30, 2009
Loading...

Share This Page