Solved

pointer to member functions

Posted on 2000-02-25
2
258 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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…

803 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