ami guru wrote:
Hello Brian,


I did not understand why down-casting is not possible. Super class can be downcasted to a concrete class through dynamic cast,
isnt it?

This is true only if you create the concrete class first. If the Group was not originally constructed as a Switch (meaning the osg::Switch::Switch() constructor was called), you can't just force it to be one. This is one of the fundamental concepts of object-oriented programming. If you don't understand this, I'd recommend grabbing a C++ book and reading through it. You really need to understand this to be able to program effectively.


To summarize, this works:

// Create Switch object
osg::Switch * sw = new osg::Switch();

// Assign to parent class pointer
osg::Group * grp = sw;

// Cast back to Switch pointer
osg::Switch * sw2 = dynamic_cast<osg::Switch *>(grp);


This doesn't work:

// Create a Group object
osg::Group * grp = new osg::Group();

// Try to cast to a Switch pointer (cast will fail and return NULL)
osg::Switch * sw2 = dynamic_cast<osg::Switch *>(grp);

--"J"

_______________________________________________
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org

Reply via email to