调用未知子级的构造函数
我想调用构造函数;
class anAbstractClass
{
public: anAbstractClass(inputdatatype){/*blablabla*/}
};
class aChield : public anAbstactClass
{
/*
...
*/
}
void _engine::initShader(_anAbstractClass** inShader)
{
*inShader = new /*???*/(inputdata for the construcor)
}
aChield* theChield;
_engine* myEngine = new _engine();
myEngine->initShader(&theChield);
那么,我如何调用 /???/ 处的构造函数? 谢谢您的答案!
I want to call the constructor;
class anAbstractClass
{
public: anAbstractClass(inputdatatype){/*blablabla*/}
};
class aChield : public anAbstactClass
{
/*
...
*/
}
void _engine::initShader(_anAbstractClass** 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好主意,但不支持在运行时获取指针的确切类型。
在您的
initShader
方法中,inShader
的类型为anAbstractClass**
并且无法获取它是指向的指针的信息方法调用之前的派生类。所以你需要改变你的代码,也许你可以使用一些工厂或类似的东西。
Nice idea, but there is no support to get the exact type of a pointer at runtime.
In your
initShader
method,inShader
is of typeanAbstractClass**
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.
你不能那样做。当
initShader
只知道基类时,它如何知道要调用哪个子构造函数?我认为你在这里想要的是一个模板化函数:
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: