My guess, without seeing the definition for the Callback class is that it has its own obj
and func
members since you access them through a Callback &
in the assignement operator. These would be hidden by the ones declared in CallbackGen. In other words, you are not accessing the right fields.
Strange Issue When Trying To Implement Callbacks To Methods In C++
-
28-09-2022 - |
Question
I am trying to implement callbacks to methods in C++.
Note: This loosely follows the code at http://www.codeproject.com/Articles/6136/Type-safe-Callbacks-in-C
I have created a Callback class and a CallbackGen class which is derived from the Callback class.
Here is a class I created to test with...
class ClassWithFunction
{
public:
void TryAndCallMe(uint32_t x)
{
std::cout << "I was called!";
}
};
And then in main()..
ClassWithFunction classWithFunction;
SlotMachine::CallbackGen<ClassWithFunction, void, uint32_t>
callBackGen(&classWithFunction, &ClassWithFunction::TryAndCallMe);
std::cout << "callbackGen.obj = " << callBackGen.obj << "\r\n"; // PRINTS 0x12345
printf("callbackGen.func = %p\r\n", callBackGen.func); // PRINTS 0x12345
SlotMachine::Callback<void, uint32_t> callBack;
// THIS LINE CALLS THE ASSIGNMENT OPERATOR
callBack = callBackGen;
However, something is going wacky when the code enters the overloaded assignment operator
Callback& operator=(const Callback<returnType, fArg1Type> &callback)
{
std::cout << "Equal operator called.\r\n";
// Check if the right-hand side Callback object has been initialised
if(&callback != NULL)
{
std::cout << "callback was not NULL.\r\n";
this->obj = callback.obj;
this->func = callback.func;
std::cout << "callback.obj = " << callback.obj << "\r\n"; // PRINTS 0
printf("callback.func = %p\r\n", callback.func); // PRINTS (nil)
std::cout << "this->obj = " << this->obj << "\r\n"; // PRINTS 0
printf("this->func = %p\r\n", this->func); // PRINTS (nil)
}
else
Callback();
return *this;
}
Those // PRINTS 0
and // PRINTS (nil)
are where I'm having issues.
The callback objects (RHS object) obj
and func
variables in the assignment operator function are NULL! Even though the "same" values were checked just before the assignment function was entered, and gave back valid memory addresses (where is says // PRINTS 0x12345
).
EDIT: To make things clearer, this is the class CallbackGen
//! @brief This can generate callbacks to member functions!
//! @details Note that this has one more type than the callback classes, that is, a type for the object that the callback function belongs to.
template<class objType, class returnType, class fArg1>
class CallbackGen : public Callback<returnType, fArg1>
{
public:
// Create method pointer type (points to method of a particular class
typedef returnType (objType::*funcT)(fArg1);
//! @brief To store the pointer to the member callback function
funcT func;
//! @brief To store the object that the callback function belongs to
objType* obj;
//! @brief Constructor
CallbackGen(objType* obj, funcT func)
{
this->func = func;
this->obj = obj;
}
protected:
CallbackGen();
};
Solution