views:

59

answers:

2

Hi! 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!

+2  A: 

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) 
}
James Curran
I was typing it as you posted it.
San Jacinto
Im curious, how can I do that so, that the user can use the initShader only with the anAbstractClass (and her childrens)?
@User408...: after the new, call `inShader->SomeFunctionDefinedInanAbstractClass()`. You'll get a compiler error if the object doesn't have that function.
James Curran
Okay, thanks for the information :)
+1  A: 

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.

IanH
Okay, I am sad a bit :'(But, the I have to look out at oodesign.comTHX for then 2 answers. This is a cool site! :)