Solved

easy function pointer question

Posted on 2007-03-22
6
243 Views
Last Modified: 2010-04-01
Hi,

I have two classes, I want to know how one class can set a pointer to a function to another like:

class A {
    void DoSomething();
    void SetFunctionPointer(?); { m_pFunctionPointer = ?; }
    void* m_pFunctionPointer;
};

class B {
     A* m_p;
     void SayHello() { cout << "Hello" << endl; }
};

void A::DoSomething()
{
    m_pFunctionPointer();
}

B::B()
{
    m_p->SetFunctionPointer(&SayHello());
}

Thanks
0
Comment
Question by:DJ_AM_Juicebox
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
If you want to call a class method, you need to keep in mind that you can only use it with an instance of a class. E.g.

#include <iostream>
using namespace std;

class B; // fwd. decl.

class A {
public:
    void DoSomething();
    void SetFunctionPointer(void (B::*ptr)()) { m_pFunctionPointer = ptr; }
    void (B::*m_pFunctionPointer)();
};

class B {
public:
     B();
     A* m_p;
     void SayHello() { cout << "Hello" << endl; }
};

void A::DoSomething()
{
    B b;
    (b.*m_pFunctionPointer)();
}

B::B()
{
    m_p->SetFunctionPointer(B::SayHello);
}

void main () {
  A a;
  a.DoSomething();

}

works for that.

0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
BTW, see also http://www.newty.de/fpt/index.html ("The Function Pointer Tutorials") for more info.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
The problem with member function pointers is that you need both the function pointer and an instance of the class the function is a member from. Because of that there are only a few known senseful samples where any one has used member function pointers, You see it in jkr's sample above. He creates a B object before calling the function pointer but the B is any B and has only default initialization. So that B object cannot do any specific things but only that which are valid for all instances of class B. for that purpose you can use static member functions which can be called without needing an instance of B but only with the class prefix:

class B {
public:
     B();
     static  void SayHello() { cout << "In B: Hello" << endl; }
};

You can call it everywhere by simply

         B::SayHello();

And function pointers to static member functions can be treated like function pointers to global functions:

// a typedef makes it simpler to define the type
typedef void (*SayFunc)();

class A
{
      SayFunc  funcPtr;   // we use our typedef here
public:
      A(SayFunc sf) :  funcPtr(sf) { }   // pass it to the constructor        
      void say() {  funcPtr(); }
};

That's all.

Regards, Alex
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
Try this :

#include <iostream>

using namespace std;

class A;

class B {
  public:
    A* m_p;
    B();
    void SayHello() { cout << "Hello" << endl; }
    ~B();
};

class A {
  private:
    void (B::*m_pFunctionPointer)();
  public:
    void DoSomething();
    void SetFunctionPointer(void (B::*fun_ptr)()) { m_pFunctionPointer = fun_ptr; }
};

void A::DoSomething() {
  B b;
  (b.*m_pFunctionPointer)();
}

B::B() {
  m_p = new A();
  m_p->SetFunctionPointer(&B::SayHello);
}

B::~B() {
  delete m_p;
}

int main(void) {
  B b;
  b.m_p->DoSomething();
  return 0;
}
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
Oops, should have checked before posting ... that's the trouble when you start writing a reply, and get distracted half way through :)
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
Comment Utility
Hey DJ,

Try this one: its tidy!
http://www.codeproject.com/cpp/Functor.asp


Best Regards,
DeepuAbrahamK
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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.

763 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

11 Experts available now in Live!

Get 1:1 Help Now