?
Solved

pointer to member functions

Posted on 2000-02-25
2
Medium Priority
?
280 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 15 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

762 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