Can we turn such structure into typed class/function?

假装没事ソ 提交于 2019-12-11 13:57:46

问题


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?


回答1:


can we turn simpleRendererGraphElement* into abstract type and make its function name we use in structure (renderCastedData) abstract too?

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:

AddSubscriberToGeneratedData(RenderCastedDataFunctor(this));

I suppose, here this represents the pointer to an instance of the concrete class. If so, then that should work!


EDIT:

I understand how good this Idea is but I do not get how to implement it. that is why I am asking.

Alright. Here is an example:

class AbstractGraphElement
{
  public:
    virtual void RenderCastedData(char* castedChar, int castedCharLength) = 0;
};

This is your abstract class, and RenderCastedData is a pure virtual function. Now you need to define a concrete class which must define RenderCastedData function. So here it is:

class SimpleGraphElement : public AbstractGraphElement
{
  public:
    virtual void RenderCastedData(char* castedChar, int castedCharLength)
    {
        //function body - define it yourself
    }
};

Done!

Now what you need to do is this. Modify RenderCastedDataFunctor as follows:

struct RenderCastedDataFunctor
{
    AbstractGraphElement* m_graphElement;

    RenderCastedDataFunctor(AbstractGraphElement* graphElement)
        : m_graphElement(graphElement) { }

    void operator()(char* castedChar, int castedCharLength)
    {
        m_graphElement->RenderCastedData(castedChar, castedCharLength);
    }
};

Then add subscriber,

AbstractGraphElement *pGraphElement = new SimpleGraphElement();
AddSubscriberToGeneratedData(RenderCastedDataFunctor(pGraphElement));

I think it gave your some idea, right? The important point is : use pointer of type AbstractGraphElement but initialize this pointer with SimpleGraphElement. I think, you should read about virtual functions, and runtime polymorphism. That would help you a lot.



来源:https://stackoverflow.com/questions/4842472/can-we-turn-such-structure-into-typed-class-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!