Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > member function pointer and STL

Reply
Thread Tools

member function pointer and STL

 
 
Maciej Kwapulinski
Guest
Posts: n/a
 
      06-24-2003
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:tr)(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:tr)(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>:tr 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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-24-2003
"Maciej Kwapulinski" <> wrote...
> hallo,
> the following program works OK:


"works OK" is not enough to be correct. It contains at least
two errors. See below.

> #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:tr)(a, b);


This is incorrect. Needs to be

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

> }
> };
>
> main() {


This is incorrect. Needs to be

int 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:


What you change your class into is not an "STL class".
It's a class template.

>
> #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:tr)(a, b); // this is line 15


return (this->*ptr)(a, b); // this is line 15

> }
> };
>
> main() {


int main() {

(as you can see both changes are _precisely_ the same as in the
non-template code)

> 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>:tr 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


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

Victor


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      06-24-2003
Maciej Kwapulinski wrote in news::

> hallo,
> the following program works OK:


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.

> #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:tr)(a, b);


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.


> }
> };
>
> 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:


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

What you have here is a class template.

>
> #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:tr)(a, b); // this is line 15


same as before:

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

> }
> };
>
> 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>:tr cannot be called
>/home/mkwap/BGP-cvs/ccc/p11.cpp:15: without an object; consider
> using .* or ->*
>


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.

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


HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointer-to-member data and pointer-to-member functions and access specifiers Stephen Howe C++ 2 11-06-2012 12:32 PM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM
Function pointer member variable to non-member function slide_o_mix C++ 0 10-15-2003 03:37 PM
function pointer and member function pointer question glen stark C++ 2 10-10-2003 01:41 PM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57