Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > Re: Still havin...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 9 of 14 Topic 4050 of 4306
Post > Topic >>

Re: Still having problems with my member function pointers

by Bart van Ingen Schenau <bart@[EMAIL PROTECTED] > Mar 6, 2008 at 06:49 PM

Andrew Falanga wrote:

> Hi,
> 
>  About two weeks ago (or so) I posted here that I was having problems
> with getting a member function pointer to point to a member function
> without giving me grief.  I've gone to trying this on a trial program
> that I will post below in its entirety.  I just can't get it right and
> although I've tried the suggestions given to me from the other
> posting, I'm still doing something wrong.  So, here's my test program:
> 
> #include <iostream>
> 
> class base {
>    int somedata;
> 
>    public:
>    base();
> 
>    int GetData() {
>       return somedata;
>    }
> 
>    int (base::*ptrMemFunc)();
> 
> };
> 
> base::base() : somedata(5), ptrMemFunc(&base::GetData)
> {}
> 
> // this one's only in here to make sure I remembered how to use
> function pointers
> int AnInt() {
>    return 5;
> }
> 
> int main( ) {
>    base b;
>    int (*pAnInt)() = AnInt;
> 
>    std::cout << pAnInt() << std::endl;
> 
>    b.ptrMemFunc();

I think part of your trouble is that you have made the pointer-to-member
itself a member of the class. This results in a requirement to mention
the object twice:

  b.*(b.ptrMemFunc)();

Some other constellations with the pointer to member function are:

  base c;
  int (base::* local_ptr)() = &base::GetData;

  b.*(c.ptrMemFunc)(): /* call the pointer in c on the object b. */
  b.*local_ptr(); /* use a local pointer-to-member */

> 
> Thanks, and forgive my confusion.
> Andy

Bart v Ingen Schenau
-- 
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.para****ft.com/c++-faq-lite/
 




 14 Posts in Topic:
Still having problems with my member function pointers
Andrew Falanga <af300w  2008-03-06 07:45:14 
Re: Still having problems with my member function pointers
"Alf P. Steinbach&qu  2008-03-06 17:27:18 
Re: Still having problems with my member function pointers
Triple-DES <DenPlettfr  2008-03-06 08:39:26 
Re: Still having problems with my member function pointers
Andrew Falanga <af300w  2008-03-06 08:41:02 
Re: Still having problems with my member function pointers
"Alf P. Steinbach&qu  2008-03-06 17:49:40 
Re: Still having problems with my member function pointers
Andrew Falanga <af300w  2008-03-06 08:56:09 
Re: Still having problems with my member function pointers
"Alf P. Steinbach&qu  2008-03-06 19:19:36 
Re: Still having problems with my member function pointers
Triple-DES <DenPlettfr  2008-03-06 09:07:53 
Re: Still having problems with my member function pointers
Bart van Ingen Schenau &l  2008-03-06 18:49:13 
Re: Still having problems with my member function pointers
Andrew Falanga <af300w  2008-03-06 10:04:26 
Re: Still having problems with my member function pointers
Triple-DES <DenPlettfr  2008-03-06 10:57:45 
Re: Still having problems with my member function pointers
Andrew Falanga <af300w  2008-03-06 11:43:17 
Re: Still having problems with my member function pointers
Andrew Falanga <af300w  2008-03-06 11:47:30 
Re: Still having problems with my member function pointers
Triple-DES <DenPlettfr  2008-03-06 22:03:46 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Oct 15 12:10:17 CDT 2008.