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();


Are you sure that ContactPtr is holding a BCorp and that it is getting cast right? Also why are you casting objects to a double * in you contact object.

>In a contact object I store a pointer, which may be for a BCorp or a BPrsn, so I cast it as a (double*).
I can't begin to fathom why you're using double* instead of void* for a safe transient pointer type. Could you explain your reasoning here?

I guess I use a (double*) because I don't know any better. I had no idea you can use a (void*) as a cast. Is it likely that changing it to (void*) would make this problem go away? Why would the compiler reinterpret a (BCorp*) cast as a (BPrsn*) cast when converting from a (double*) cast? Or, maybe, that doesn't matter. Why would using a (void*) cast avoid such problems? Nor have I seen the wording 'transient pointer'. I will research that. I will also try the (void*) option. I appreciate your response.

>I had no idea you can use a (void*) as a cast.
void* is the generic pointer type. You're pretty much guaranteed to be able to convert a pointer to and from void* without any problems. There's no such guarantee for any other arbitrary combination of pointer types. Performing a safe typecast is your first step in troubleshooting the problem.

>Nor have I seen the wording 'transient pointer'.
It's not official terminology. In fact, I made it up on the spot. ;)

I will try it and let you know the result tomorrow.

Thank you Oliver and Narue.

The problem has been fixed. The due to an error in logic the original pointer, which was (BPrsn*), was converted to (double*) as intended, but then converted to (BCorp*). On suggestion by Oliver, I dug into the code and discovered this. Evidently Intellisense tracked the original type, through two casts. I am mystified how it did this, but OK.

On the advice of Narue I have also changed my use of (double*) as a type for storing untyped pointer, and replaced it with (void*). I am unsure of the benefits of this, but if I am closer to standard practice, that would probably be a good thing.

Thanks for your help.

G Boyle