Solved

pointer to member functions

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 how to clear a vector as well as how to detect empty vectors in C++.

707 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