开发者

Call the constructor of the unknown children

开发者 https://www.devze.com 2023-01-11 23:30 出处:网络
I want to call the constructor; class anAbstractClass { public: anAbstractClass(inputdatatype){/*blablabla*/}

I want to call the constructor;

class anAbstractClass
{
 public: anAbstractClass(inputdatatype){/*blablabla*/}
};

class aChield : public anAbstactClass
{
/*
...
*/
}

void _engine::initShader(_anAbstr开发者_JAVA技巧actClass** inShader)
{
    *inShader = new /*???*/(inputdata for the construcor)
}

aChield* theChield;
_engine* myEngine = new _engine();
myEngine->initShader(&theChield);

So, how can I call the constructor at the /???/? Thx ahead for the answers!


You cannot do that. How is initShader going to know which child constructor to call, when all it knows is the base class?

What I think you want here, is a templated function:

template <typename T>
void _engine::initShader(T ** inShader) 
{ 
    *inShader = new T(inputdata for the construcor) 
}


Nice idea, but there is no support to get the exact type of a pointer at runtime.

In your initShader method, inShader is of type anAbstractClass** and there is no way to get the information that it was a pointer to pointer to a derived class before the method call.

So you need the change your code, maybe you can use some Factory or something like this.

0

精彩评论

暂无评论...
验证码 换一张
取 消