Problem with inheritance

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Manawenuz
Posts: 22
Joined: Wed Jun 10, 2009 12:42 am
Location: France

Problem with inheritance

Post 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?
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post 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... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Manawenuz
Posts: 22
Joined: Wed Jun 10, 2009 12:42 am
Location: France

Post 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
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post 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 ^^
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
Manawenuz
Posts: 22
Joined: Wed Jun 10, 2009 12:42 am
Location: France

Post by Manawenuz »

Too bad. But OK Thx for your answers!
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post 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
Image
Image
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post 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.
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post 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... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
B@z
Posts: 876
Joined: Thu Jan 31, 2008 5:05 pm
Location: Hungary

Post by B@z »

ah i see, sorry, i misunderstood it xD
Image
Image
Post Reply