Solved

pointer to member functions

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now