Hi,
I'm fairly new to C++ (old Ada guy), and I'm having a problem which I hope you experts can help me resolve. I am implementing a callback functionality. Here are my template classes:
Callback<CollectionElementCallbackParameter*, OwcScanController>* m_ResourceManagerCallback = new Callback<CollectionElementCallbackParameter*, OwcScanController>( *this, &OwcScanController::handleResourceManagerCallbacks );
Then I call a function to maintain a list of pointers to "registered" callbacks that I use to dispatch the callback when some event occurs using this code:
1 2 3 4 5 6 7 8 9 10 11
// In the ResourceManager.hpp class header file
static list<CallbackBase<CollectionElementCallbackParameter*>*> m_CallbackList;
// In the ResourceManager.cpp class implementation file
void ResourceManager::AddClasswideCallback(CallbackBase<CollectionElementCallbackParameter*>& theCallback)
{
CallbackBase<CollectionElementCallbackParameter*>* theCallbackCopy = theCallback.clone();
bool test = (*theCallbackCopy == theCallback);
m_CallbackList.push_back(theCallbackCopy);
}
The problem is that the == operator always executes the virtual operator== function in the CallbackBase class, and not then one in my derived Callback class as I need it to do. I actually need the derived == function in the ResourceManager::RemoveClasswideCallback(CallbackBase<CollectionElementCallbackParameter*>& theCallback) function, but I didn't want to muddy the waters even more.
Thanks Much for any help, I've spent hours searching the Web and can't find a single example of operator== overloading in derived template classes.
It can be done, but it is ugly. The problem is that the derived class' operator== does not have the same signature
as the base class' operator==, and therefore is not an override.
Do you need to be able to compare callbacks? I'm not sure line 42 is going to do what you want it to do
anyway. I think you want to check if theClient refers to the same object as other.theClient, but that is not what
the code checks.
If you don't need the comparison, then you can simplify the code a lot by using boost::function and boost::bind
instead (assuming you have access to the boost libraries) because they provide you with the same functionality
and a lot more.
I realized that the derived class' operator== does not have the same signature as the base class' operator==, but since it is a derived from the base, and the == operator needs to take an object of its own class type, I thought the compiler would know that it overrides the virtual BaseClass == operator. I don't have access to boost (not allowed to use Open Source code), and I already have a large code base using this implementation, but I'm debugging the RemoveClasswideCallback() function, and my comparison (like the bool test line 9) doesn't work since it can't get into line 42 of the derived class code using objects of the base class type (CallbackBase).
I think line 42 is correct, it checks that they are of the same class, and pointing to the same member function. At least that was my intention ;-)
Is it not possible for a derived class to override the base class == operator, and make use of its own member data in the comparison function? Like it the standard examples where base class is a Shape and the derived class is a Circle, is there a way to have the Circle operator==(const Circle& myShape) override Shape::==(const Shape& myShape) so you can compare two objects of type Shape, be they Circles (comparison uses radius member data), or Rectangles (comparison uses width and height member data) and have the correct behavior?