Strange dynamic_cast problem
Strange dynamic_cast problem
- Subject: Strange dynamic_cast problem
- From: Eric Gorr <email@hidden>
- Date: Mon, 3 Nov 2008 17:09:16 -0500
I have a strange problem with a dynamic_cast.
I have put together a simple test project which can be found at:
http://ericgorr.net/LibraryLoading.zip
which demonstrates the problem.
Unfortunately, this is a bit complicated, but I will do my best to
explain this simply.
In Shared.h, there are the definitions of two purely virtual classes -
A & B. B is a subclass of A.
In LibraryA, there is a implementation of class B called BImp. There
is a function called GetA which returns a pointer to an instance of
BImp and returns an A*.
In LibraryB, there is a function called test. This function takes a
void *, which will end up being a function pointer to the GetA
function from LibraryA.
The problem comes in the test function with the line:
B* myB = dynamic_cast<B*> (myA);
The dynamic_cast fails and myB is assigned NULL. This _should not_
fail because myA is an instance of class B.
However, I can make this dynamic_cast succeed, if in main.cp, which is
a part of the Application target, I set:
#define CASE_A 1
which allows
A *myA = functionPtr();
B *myB = dynamic_cast<B*> (myA);
to be executed before the test function from LibraryB is called.
Any idea why this allows it to work?
Any idea why it is failing in the first place?
Any thoughts or comments would be appreciated.
p.s.
In main.cp, there are two #define's.
#define CASE_A 0
#define CASE_B 0
If both are set to zero, it will crash. This is simplest crashing case.
If CASE_A is 1, it will work.
If CASE_B is 1, it will crash and this is the case closest to the real
case I am working with.
Of course, both CASE_A & CASE_B cannot be 1.
thank you.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden