Download

Function pointers

Hi, so i have this code for a shop (this shop interface can be spawned by any class). When the user confirms the purchase, this interface handles the purchase (like substracts some coins from the save file and this kind of stuff) and then calls “PurchaseSuccesfullFunc”. However, i get this error:

Error C2440 ‘=’: cannot convert from ‘void (__cdecl UShipyardMenu::* )(void)’ to ‘void (__cdecl *)(void)’

I’m not exactly an expert with C++ and i’m kinda stuck, can anyone explain to me what’s happening and how can i fix this? Thanks.

UBuyInterface.h



class PIRATES_API UBuyInterface : public UUserWidget
{
GENERATED_BODY()

public:
void (*PurchaseSuccessfulFunc)() = ]() {};

};


UBuyInterface.cpp



void UBuyInterface::ConfirmPressed()
{
PurchaseSuccessfulFunc();
}


some other class (ShipyardMenu.cpp)



void UShipyardMenu::UpgradeHullPressed()
{
UBuyInterface* BuyInterface = CreateWidget<UBuyInterface>(this, BuyInterfaceClass);
BuyInterface->AddToViewport();
BuyInterface->PurchaseSuccessfulFunc = &UShipyardMenu::UpgradeHullSuccessful; //this function is a void function with no parameters
}


P.S.: After writing this post i managed to make the interface work by making UpgradeHullSuccessful a global function, but i bet there is a better way to to this, since this just feels disorganised.

Pointer to function is different from pointer to member function. Signature of first is void(*)(void), second is void(Class::*)(void).
Size of pointer-to-member-function is implementation-defined and cant be easily converted to plain pointer-to-function.

If you want your callback to be able to bind to both, a free/static function and a class member function then use TFunction or TFunctionRef