The goal is to have the member variable _AddValue
point to the CreateFirstValue
function upon class initialization and after the first invocation of AddValue
, all future calls to it will invoke CreateAnotherValue
.
Previously, I just had a single AddValue
function with a conditional check to determine which function to call. However, I feel like that implementation is flawed because that if
check will occur every time and it seems like a function pointer would be beneficial here.
An example:
class Foo
{
private:
int _value;
void (*_AddValue)(int value); // Pointer to function member variable
void CreateFirstValue(int value)
{
_value = value;
_AddValue = &CreateAnotherValue;
}
void CreateAnotherValue(int value)
{
// This function will create values differently.
_value = ...;
}
public:
// Constructor
Foo()
: _value(0), _AddValue(CreateFirstValue)
{
}
AddValue(int value) // This function is called by the user.
{
_AddValue(value);
}
};
The code above is not the actual code, just an example of what I'm trying to accomplish.
right now I'm getting an error: argument of type void (BTree::)(int) does not match void (*)(int)