| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Dave" |
| Date: |
29 Apr 2004 12:58:25 PM |
| Object: |
Pointers to member functions and default parameters |
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;
}
.
|
|
| User: "Christopher Benson-Manica" |
|
| Title: Re: Pointers to member functions and default parameters |
30 Apr 2004 07:58:13 AM |
|
|
Dave <better_cs_now@yahoo.com> 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.
.
|
|
|
| User: "Dave" |
|
| Title: Re: Pointers to member functions and default parameters |
30 Apr 2004 09:52:47 AM |
|
|
"Christopher Benson-Manica" <ataru@nospam.cyberspace.org> wrote in message
news:c6tih5$p3u$3@chessie.cirr.com...
Dave <better_cs_now@yahoo.com> 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...
.
|
|
|
|
|

|
Related Articles |
|
|