Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

dynamic_cast

This topic is 5154 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys! shouldn't this dynamic cast work?
class ISystem {
public:
	virtual void SysTest() = 0;
};

class CSystem : public ISystem {
public:
	void SysTest() {
		MessageBox(NULL,"System WORKS", "System WORKS", MB_OK);
	}
};

class TestInterface {
public:
	virtual void SomeFunction() = 0;
};

class TestImplementation : public TestInterface, public CSystem {
public:
	void SomeFunction() {
		MessageBox(NULL,"TestInterface WORKS", "TestInterface WORKS", MB_OK);
	}
};

//then I want to do some dynamic casts:

ISystem* pSystem = new TestImplementation();
//For what I know this should work shouldnt it?

TestInterface* pTestInterface = dynamic_cast<TestInterface*>(pSystem);
//And if not that then at least this one?

CSystem* pSystem2 = new TestImplementation();
TestInterface* pTestInterface = dynamic_cast<TestInterface*>(pSystem2);

The compiler is giving me this warning: warning C4541: 'dynamic_cast' used on polymorphic type 'ISystem' with /GR-; unpredictable behavior may result And when I run the application I'm getting a "Runtime error"... Does anyone know what this means? I thought this was the purpose of dynamic_cast ? thank you! [edited by - bilsa on April 12, 2004 9:08:27 AM]

Share this post


Link to post
Share on other sites
Advertisement
He, thats right... forgot that one. But still the application craches on the dynamic_cast

I mean, it shouldn't make a "Runtime error" right?!
It should return a NULL in case it failes.


This is how I'm calling it more exactly:


//the ISystem* tmpSys interface pointer is filled...

if(tmpSys != NULL) {
//now test if the interface can be used?

tmpSys->SysTest();
MessageBox(NULL,"The ISystem pointer works fine!","test",MB_OK);

//try casting it to the desired interface

TestInterface* pTestInterface = dynamic_cast<TestInterface*>(tmpSys);
MessageBox(NULL,"The dynamic cast works fine!","1",MB_OK);

if(pTestInterface != NULL) {
pTestInterface->Message();
}
}


And the the Runtime error comes just before the second MessageBox...

[edited by - bilsa on April 12, 2004 9:46:56 AM]

Share this post


Link to post
Share on other sites
Ok, the problem was that since the tmpSys pointer pointed to an object created in a DLL, and the DLL project didnt have RTTI enabled... only the engine had it enabled, the application failed...

Works just fine now...

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!