• Advertisement

Archived

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

dynamic_cast ?

This topic is 5151 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

Hi, I''m having problem with a dynamic cast, which by my opinion should be succesfull:
class BaseC{
public:
	BaseC() {MessageBox(NULL,"BaseC Constructor", "Constructor", MB_OK);}
	virtual ~BaseC() {MessageBox(NULL,"BaseC Destructor", "Destructor", MB_OK);}
};

class DerivedC : public BaseC {
public:
	DerivedC():m_val("Works!") {MessageBox(NULL,"DerivedC Constructor", "Constructor", MB_OK);}
	virtual ~DerivedC() {MessageBox(NULL,"DerivedC Destructor", "Destructor", MB_OK);}
	void TestFunc() {MessageBox(NULL,m_val, "Constructor", MB_OK);}
private:
	char* m_val;
};

//then:


	 BaseC *baseObject = new DerivedC();

	 DerivedC *derivedObject = dynamic_cast<DerivedC*>(baseObject);
	 
	 if(derivedObject != NULL) {
		derivedObject->TestFunc();
	 }
This gives me a RunTime error on the dynamic_cast. But from what I have read it is supposed to work like this?? thx!

Share this post


Link to post
Share on other sites
Advertisement
The code looks fine. What kind of runtime error are you getting?

Share this post


Link to post
Share on other sites
"Microsoft Visual C++ Runtime Library"
"
Runtime Error!

Program: ...

This application has requested the Runtime to terminate it in an unusual way.
Please contact the applications support team for more information."

I''m using VC++ .NET 2003
WinXP

Share this post


Link to post
Share on other sites
Have you enabled runtime type info on your compiler? At least in VC++.NET 2002 RTTI is not enabled on default.

"Life is very short, and there''s no time for fussing and fighting my friend..."

Share this post


Link to post
Share on other sites

  • Advertisement