bcf
bcf

Reputation: 2134

Initialize object pointer in another class with function pointer

Consider the following. Class A has a function pointer as a member and accepts a function in its constructor to pass to this member. In a separate file, I have a class B that contains a pointer to class A as a member, and class B also has as a member the function I want to pass to class A.

Below is an example and the errors I receive. What's the standard method of doing something like this?

A.h:

class A {
 private:
  int (*func)(int);

 public:
  A(int (*func_)(int));
};

A::A(int (*func_)(int)) : func(func_) {}

B.h:

#include "A.h"  // Why can't I forward declare A instead?

class B {
 private:
  A *A_ptr;
  int function(int);  // some function

 public:
  B();
  ~B();
};

int B::function(int n) {
  return n+2;  // some return value
}

B::B() {
  A_ptr = new A(function);
}

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

main.cpp:

#include "B.h"

int main() {
  B b;
}

Errors I get:

B.h: In constructor ‘B::B()’:
B.h:18:25: error: no matching function for call to ‘A::A(<unresolved overloaded function type>)’
B.h:18:25: note: candidates are:
A.h:9:1: note: A::A(int (*)(int))
A.h:9:1: note:   no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘int (*)(int)’
A.h:1:7: note: A::A(const A&)
A.h:1:7: note:   no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘const A&’

Upvotes: 0

Views: 599

Answers (1)

Matthew
Matthew

Reputation: 2829

To answer your question regarding "What's the standard method of doing something like this" I'll assume you mean passing member functions and/or general function pointers around and executing them with some data. Some popular implementations which provide this ability are:

  1. FastDelegate
  2. std::function
  3. boost::function

It really comes down to preference and library choice. Personally, I've used FastDelegate most of the time and then std::function after that.

All the links I posted should have tutorial information to get you up and running and show you how to properly pass and store member functions and/or general function pointers with ease.

Here's an example of using a FastDelegate with your example:

class A 
{
public:
    // [1] This creates a delegate type. Can used for any function, 
    // class function, static function, that takes one int 
    // and has a return type of an int.
    typedef FastDelegate1< int, int > Delegate;

    // [2] Pass the delegate into 'A' and save a copy of it.
    A( const Delegate& delegate ) : _delegate( delegate ) { };

    void execute()
    {
        // [7]
        // Result should be 10!
        int result = _delegate( 8 ); 
    }

private:
    // [3] Storage to save the Delegate in A.
    Delegate _delegate;
};

class B
{
public:
    B() 
    {
        // [4] Create the delegate
        A::Delegate bDelegate;
        bDelegate.bind( this, &B::function );

        // [5] Create 'A' passing in the delegate.            
        _aPtr = new A( bDelegate );

        // [6] Test it out!! :) 
        // This causes `A` to execute the Delegate which calls B::function. 
        _aPtr->execute();
    }

    ~B() 
    {
        delete _aPtr; 
    }

    int function( int n )
    {
        return n+2;
    }

private:
    A* _aPtr;
};

Upvotes: 1

Related Questions