问题描述
所以在结构上像
struct RenderCastedDataFunctor
{
simpleRendererGraphElement * obj_;
RenderCastedDataFunctor(simpleRendererGraphElement * obj)
:obj_(obj){}
void operator()(char * castedChar,int castedCharLength)
{
obj _-> renderCastedData(castedChar,castedCharLength);
}
};
我们可以将simpleRendererGraphElement *转换为抽象类型,并在结构中使用它的函数名$ c> renderCastedData )抽象?
所以我有一个 charGenerator
class
template< typename Function>
void AddSubscriberToGeneratedData(function f)
我想传递给它的函数来自不同的类类型void(differentClass :: *)(char *,int)
有了这个结构里面一些simpleRendererGraphElement对
charGenerator-> AddSubscriberToGeneratedData(RenderCastedDataFunctor(this));
$的数据调用 renderCastedData
b $ b我想有一个方法能够传递抽象类函数,它使 char *
和 int
AddSubscriberToGeneratedData
。如何做这样的事情?
非常好的主意。你应该做这个。通过使函数 virtual
创建类的抽象,然后定义一个实现虚函数的具体类(从这个抽象类派生)。这将是一个更好的设计!
其余似乎已经很好。你不必做任何事情,因为你这样做:
AddSubscriberToGeneratedData(RenderCastedDataFunctor(this));
我想这里这个
到具体类的实例。如果是这样,那应该可以工作!
我理解这个想法是多么好,但
我没有得到如何实现它。
是为什么我问。
好的。这里是一个例子:
class AbstractGraphElement
{
public:
virtual void RenderCastedData char * castedChar,int castedCharLength)= 0;
};
这是你的抽象类, RenderCastedData
是一个纯虚函数。现在你需要定义一个具体的类,它必须定义 RenderCastedData
函数。所以这里是:
class SimpleGraphElement:public AbstractGraphElement
{
public:
virtual void RenderCastedData(char * castedChar,int castedCharLength)
{
//函数体 - 自己定义
}
};
完成!
做到这一点。修改 RenderCastedDataFunctor
如下:
struct RenderCastedDataFunctor
{
AbstractGraphElement * m_graphElement;
RenderCastedDataFunctor(AbstractGraphElement * graphElement)
:m_graphElement(graphElement){}
void operator()(char * castedChar,int castedCharLength)
{
m_graphElement-> RenderCastedData(castedChar,castedCharLength);
}
};
然后添加订阅者,
AbstractGraphElement * pGraphElement = new SimpleGraphElement();
AddSubscriberToGeneratedData(RenderCastedDataFunctor(pGraphElement));
我想它给了你一些想法,对吧?重要的是:使用 AbstractGraphElement
类型的指针,但是用 SimpleGraphElement
初始化这个指针。我想,你应该阅读关于虚拟函数和运行时多态性。这将帮助你很多。
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 withcharGenerator->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:
AddSubscriberToGeneratedData(RenderCastedDataFunctor(this));
I suppose, here this
represents the pointer to an instance of the concrete class. If so, then that should work!
EDIT:
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.
这篇关于我们可以把这样的结构转换成类型/函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!