[info-performer] dynamic_cast

New Message Reply Date view Thread view Subject view Author view

From: Steffen (sb123++at++gmx.de)
Date: 02/04/2003 02:02:09


Since pfNode is polymorph
    virtual ~pfNode();
and pfDCS inherits from it via pfSCS and pfGroup,
I do not understand the following crash using dynamic_cast for safety:
(pfNode* ch; inserted to find the crash)

        pfNode* gExt = pfdLoadFile("any.flt");
        if (gExt) {
                //make instance
                pfGroup* inst = new pfGroup;
                pfDCS* d = new pfDCS;
                inst->addChild(d);
                d->addChild(gExt);

                //clone 'inst' and move
                unsigned int n = 100;
                std::vector<pfGroup*> g(n, (pfGroup*)(0));
                for (unsigned int i=0; i<n; i++) {
                        g[i] = (pfGroup*)inst->clone(0);
                        pfNode* ch = g[i]->getChild(0);

                        pfDCS* d = (pfDCS*)(ch); //works
// d = dynamic_cast<pfDCS*>(ch); //crashes!

                        if (d) d->setTrans(4, 3*i, 0);
                        m_pGTerrain->addChild(g[i]);
                }
        }

Any hint?
I'm using MSVC++ 6 and RTTI is on - works with other classes.

Steffen


New Message Reply Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Tue Feb 04 2003 - 02:04:16 PST

This message has been cleansed for anti-spam protection. Replace '++at++' in any mail addresses with the '@' symbol.