Solved

pointer to member functions

Posted on 2000-02-25
2
272 Views
Last Modified: 2010-04-02
#include <stdio.h>
class a
{
public:
 void      funca();
};
class b
{
public:
void   funcb();
};

void a::funca() { printf("aaaaaaaaaaaaaa\n");};
void b::funcb() { printf("bbbbbbbbbbbbbb\n");};

class c:public a,public b
{
public:
      typedef void (c::*FUNC)();

      c(int aint){if (aint==0)
                  funcc=&c::funca;
                  else
                  funcc=&c::funcb;
                  
      };
      ~c(){};
public:
      void stubfunc(){(this->*funcc)();};//why need this-> ?
public:
   
      FUNC funcc;
};

void main()
{
      c ac(0),bc(1);
      (ac.*ac.funcc)();// why not be ac.*funcc()? more details will
                         // be appreciated.
      (ac.*bc.funcc)();// it's right! why?
      (bc.*ac.funcc)();
      (bc.*bc.funcc)();
      ac.stubfunc();
      bc.stubfunc();

}
0
Comment
Question by:anonexperts
2 Comments
 
LVL 5

Expert Comment

by:Wyn
ID: 2560206
hi,anonexperts:

one trival hint:

All non-static functions need a specific valid class instance to work with.

one friendly advice:

Here many experts on this field ,but you'd better give more points(at least 50pt).That will interst them to answer you and you will get more help.It's worthy.

Regards
W.Yinan

0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 5 total points
ID: 2560767
>void stubfunc(){(this->*funcc)();};//why need this-> ?

->* is required to dereference the function pointer.

funcc() would not be enough.

You alwas need a LHS for the ->* (or .*) member-function pointer.  And in this case, it is a member of ourself (ie. this) that we want to call.

>(ac.*ac.funcc)();// why not be ac.*funcc()? more details will
>                         // be appreciated.

ac.*funcc would mean there is a local var called funcc that is a pointer-to-memeber-function.  In this case, the pointer to memeber function is a member of ac, so you need ac.*ac.func

>(ac.*bc.funcc)();// it's right! why?

yes .. this looks at bc to see which memeber function bc.funcc is pointing to, and then invokes that function for object ac.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question