Page 1 of 1

Problem with inheritance

Posted: Wed Jul 08, 2009 12:08 am
by Manawenuz
Hi everybody,

I'm trying to render the solar system (like in all my posts :)), and my class Planet is derivated from IMeshSceneNode

Code: Select all

class Planete : public scene::IMeshSceneNode {
protected:
	(some stuff)
	...
public:
	(some other stuff)
        ...
};
The problem (according to the compilator) is when I try:

Code: Select all

Planete* earth = smgr->addSphereSceneNode(6378137., 64, 0, 1, core::vector3df(), core::vector3df(), core::vector3df(1.0f, 1.0f, 1.0f));
Of course, it works if I do

Code: Select all

scene::IMeshSceneNode* earth = smgr->addSphereSceneNode(6378137., 64, 0, 1, core::vector3df(), core::vector3df(), core::vector3df(1.0f, 1.0f, 1.0f));
Any idea?

Posted: Wed Jul 08, 2009 1:32 am
by Acki
well, a cast would help (or not, see bottom):

Code: Select all

Planete* earth = (Planete*)smgr->addSphereSceneNode(6378137., 64, 0, 1, core::vector3df(), core::vector3df(), core::vector3df(1.0f, 1.0f, 1.0f));
but I doubt this works, because you create a sphere node and not your planete node !!! :shock:
you'll have do create it this way:

Code: Select all

Planete* earth = new Planete(...)
and of course you'll have to delete it on your own when it's no longer needed... ;)

Posted: Wed Jul 08, 2009 11:18 am
by Manawenuz
But why

Code: Select all

scene::IMeshSceneNode* earth = smgr->addSphereSceneNode(6378137., 64, 0, 1, core::vector3df(), core::vector3df(), core::vector3df(1.0f, 1.0f, 1.0f));
works, as well as

Code: Select all

scene::ISceneNode* earth = smgr->addSphereSceneNode(6378137., 64, 0, 1, core::vector3df(), core::vector3df(), core::vector3df(1.0f, 1.0f, 1.0f));
?
It would be really helpfull to me

Posted: Wed Jul 08, 2009 11:59 am
by Sylence
Because IMeshSceneNode inherits ISceneNode and the addSphereSceneNode() method returns an IMeshSceneNode which can be casted to ISceneNode.

ISceneNode cannot be castet to IMeshSceneNode (well it can but it can't if it isn't an IMeshSceneNode)

So if you create a function that returns a Planete* you can also cast it to IMeshSceneNode and ISceneNode but not the other way round.

As far as I know there is a term that describes this that I can't remember atm ^^

Posted: Wed Jul 08, 2009 12:03 pm
by Manawenuz
Too bad. But OK Thx for your answers!

Posted: Wed Jul 08, 2009 12:44 pm
by B@z
wait but it works with animatedmeshscene node isnt it!?

for example, you add animated mesh with
ISceneManager->addAnimatedMeshSceneNode, then store that in an ISceneNode.

later that you cast it to IAnimatedMeshSceneNode, and for example get its joints (ISceneNode doesnt have joints), then it returns the true value

Code: Select all

ISceneNode* node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("blahblah.b3d"));

vector3df bonePos = ((IAnimatedMeshSceneNode*)node)->getJointNode(1)->getPosition();
this one should work (didnt try it now, but using one like this)

sorry if i misunderstood the problem xD

Posted: Wed Jul 08, 2009 1:03 pm
by Sylence
Well then addAnimatedMeshSceneNode() internally creates an IAnimatedMeshSceneNode() but returns the results as ISceneNode.

Of couse you can cast everything to anything but this will not work if the cast is not intended.

Posted: Wed Jul 08, 2009 1:27 pm
by Acki
B@z wrote:wait but it works with animatedmeshscene node isnt it!?

for example, you add animated mesh with
ISceneManager->addAnimatedMeshSceneNode, then store that in an ISceneNode.

later that you cast it to IAnimatedMeshSceneNode, and for example get its joints (ISceneNode doesnt have joints), then it returns the true value

Code: Select all

ISceneNode* node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("blahblah.b3d"));

vector3df bonePos = ((IAnimatedMeshSceneNode*)node)->getJointNode(1)->getPosition();
this one should work (didnt try it now, but using one like this)

sorry if i misunderstood the problem xD
that's right, because IAnimatedMeshSceneNode is inherited from ISceneNode...
but it won't work the other way:
if you create an ISceneNode (addSceneNode) you can't cast it later to an IAnimatedMeshSceneNode !!!
well, in fact you can, the compiler will allow this, but it won't work, because the created ISceneNode doesn't know about methodes that the IAnimatedMeshSceneNode has, because ISceneNode is not inherited from IAnimatedMeshSceneNode...

EDIT: it's a bit like with programs, eg:
you have a program that saves and loads something...
now you get a newer version of this program...
the new version usually can read the datas the old version wrote...
but because there are new features to the new version, the old version can't read the contents of the new version...
you could say the new version is inherited from the old version, but the old version (of course) not from the new version, so it can't know what new features the new version has... ;)

Posted: Wed Jul 08, 2009 3:37 pm
by B@z
ah i see, sorry, i misunderstood it xD