CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Threaded View

  1. #1
    Join Date
    Mar 2010
    Location
    Mexico
    Posts
    9

    Function Pointers

    First of all, thank you in advance for your time.

    Can I use a function pointer but when that function belongs to an object in a given class?

    For example:

    Code:
    //// Example.h
    
    #ifndef EXAMPLE_H_
    #define EXAMPLE_H_
    
    class Example {
    public:
    	int funcA(int, int);
    	static int staticFuncB(int a, int b);
    };
    
    #endif
    Code:
    //// Example.cpp
    
    #include "Example.h"
    
    int Example::funcA(int a, int b) {
    	return a * b;
    }
    
    int Example::staticFuncB(int a, int b) {
    	return a * b;
    }
    Code:
    //// Main.cpp
    
    #include <stdio.h>
    #include "Example.h"
    
    int ejecutaFunc(int a, int b, int (*func)(int, int)) {
    	return (*func)(a, b);
    }
    
    int main() {
    	Example * a = new Example();
    	int resp1 = a->funcA(2, 3);
    	int resp2 = ejecutaFunc(2, 3, Example::staticFuncB);  // Works
    	//int resp2 = ejecutaFunc(2, 3, a->funcA); // Failure
    
    	delete a;
    
    	return 0;
    }

    Is there any way to generate this behavior?

    Code:
    int resp2 = ejecutaFunc(2, 3, a->funcA);
    Thank you
    Last edited by xlarsx; March 5th, 2010 at 09:29 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured