So in structure like
struct RenderCastedDataFunctor
{
simpleRendererGraphElement* obj_;
RenderCastedDataFunctor(simpleRendererGraphElement* obj)
: obj_(obj) { }
void operator()(char* castedChar, int castedCharLength)
{
obj_->renderCastedData(castedChar, castedCharLength);
}
};
can we turn simpleRendererGraphElement* into abstract type and make its function name we use in structure (renderCastedData) abstract too?
So I have a function inside charGenerator class
template <typename Function>
void AddSubscriberToGeneratedData(Function f)
I want to pass to it functions from different classes of type void (differentClass::*)(char*, int)
With that structure inside some simpleRendererGraphElement I can subscribe function called renderCastedData to data with
charGenerator->AddSubscriberToGeneratedData(RenderCastedDataFunctor(this));
I want to have a way to be capable to pass abstract class function that takes char* and int to AddSubscriberToGeneratedData. How to do such thing?
Very very good idea. You should do this. Make the class abstract by making it’s functions
virtual, and then define a concrete class (deriving from this abstract class) which implements the virtual functions. That would be a better design!And the rest seems already fine. You don’t have to do anything, as you’re doing this:
I suppose, here
thisrepresents the pointer to an instance of the concrete class. If so, then that should work!EDIT:
Alright. Here is an example:
This is your abstract class, and
RenderCastedDatais a pure virtual function. Now you need to define a concrete class which must defineRenderCastedDatafunction. So here it is:Done!
Now what you need to do is this. Modify
RenderCastedDataFunctoras follows:Then add subscriber,
I think it gave your some idea, right? The important point is : use pointer of type
AbstractGraphElementbut initialize this pointer withSimpleGraphElement. I think, you should read about virtual functions, and runtime polymorphism. That would help you a lot.