Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Function Pointers or Delegates

Hi All,

Another C++ noob question here which I would appreciate some input on.

I have the following situation:

I have 2 classes, let's call them The Boss and The Worker.

The Boss creates a new instance of The Worker, and instructs The Worker to start doing its thing. Now when certain conditions are met in The Worker, it needs to post back to The Boss telling it that something's happened.

Now, I'm looking to make The Worker generic, in the sense that when The Boss instructs The Worker to start doing its thing, it must also give it a reference to one of the Boss' functions which must be called when The Worker wants to inform The Boss of something happening.

I've tried getting function pointers as well as delegates working but I'm going on 2 hours here without any results, so any help would be greatly appreciated.

I suppose my question officially is: What approach should I use to solve this, and example code would be greatly appreciated.


Product Version: Not Selected
more ▼

asked Apr 09 '14 at 07:43 PM in C++ Programming

avatar image

2.6k 95 41 111

avatar image User-658380556 Jun 07 '14 at 11:50 AM

Delegates ARE function pointers, with some extra functionality built in.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Wiki Tutorial on Function Pointers

I wrote an entire tutorial on Function Pointers!




The Code, not as pretty as on wiki

 #include "YourClass.generated.h"
 #define BEHAVIORS_MAX 30
 class AYourClass : AActor //or any other class setup
     //The Function Pointer Variable Type
     //Functions take in 0 parameters and return void
     typedef void (AYourClass::*FunctionPtrType)(void);
     //A static array of 30 Function Pointers
     FunctionPtrType BehaviorFunctions[BEHAVIORS_MAX];
     //Play a Behavior from the Function Pointer Array
     //    implementation does not vary in subclasses, so not virtual
     void PlayBehavior(int32 BehaviorIndex );
     //Initialize the array
     void InitBehaviors();
     //The actual functions which are implemented in subclasses
     //or this class itself
     virtual void PlayBehavior_Idle_LookLeftToRight();
     virtual void PlayBehavior_Idle_LookFullCircle();
     virtual void PlayBehavior_Idle_ScanUpDown();
     //...more functions
 #define SCAN_UP_DOWN      2
 //... more function index defines to 29
 void AYourClass::InitBehaviors()
     BehaviorFunctions[LOOK_FULL_CIRCLE] = &AYourClass::PlayBehavior_Idle_LookFullCircle;
     BehaviorFunctions[SCAN_LEFT_TO_RIGHT] = &AYourClass::PlayBehavior_Idle_LookLeftToRight;
     BehaviorFunctions[SCAN_UP_DOWN] = &AYourClass::PlayBehavior_Idle_ScanUpDown;
     //...more functions
 void AYourClass::PlayBehavior_Idle_LookLeftToRight(){}
 void AYourClass::PlayBehavior_Idle_LookFullCircle(){}
 void AYourClass::PlayBehavior_Idle_ScanUpDown(){}
 //...rest of functions
 void AYourClass::PlayBehavior(int32 BehaviorIndex )
     //valid range check
     if (BehaviorIndex >= BEHAVIORS_MAX || BehaviorIndex < 0) return;
     //Special Thanks to Epic for this Syntax
     (this->* (BehaviorFunctions[BehaviorIndex]))();
     //the above line plays the appropriate function based on the passed in index!
more ▼

answered Apr 09 '14 at 09:47 PM

avatar image

10.6k 446 346 1088

avatar image staticvoidlol Apr 10 '14 at 06:50 AM

Rama you are a legend. Thanks!

P.S. I see the page was tagged as [Category:Code], so that's why I didn't find anything on the wiki.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question