I am writing a model economy in which corporations (class BCorp) and persons (class BPrsn) act as agents buying and selling goods and services. I have a pool of contacts (class BContact) in which contact information is retained about each contact between agents. In a contact object I store a pointer, which may be for a BCorp or a BPrsn, so I cast it as a (double*). When I need to access a pointer, I recast it as (BCorp*) or (BPrsn*) as appropriate.
However, at some point things get confused and the program ASSERTS(). When I use intellisense in debug mode I see the following happening:
- I declare a BCorp* and set it to null (intellisense says it is a null BCorp*);
- I access a pointer from a contact (a double*) and cast it to (BCorp*);
- The moment it is so cast, intellisense now says it is a (BPrsn*) pointer, and all of the contents at that pointer become nonsensical.
The line of code in which this happens looks like this:
CorpPtr = (BCorp*) ContactPtr->GetAgentDPtr();
Here is my question: WHAT WOULD CAUSE A CAST TO FAIL, AND ASSIGN A DIFFERENT CAST, AT RUN-TIME?