Run-time casting error

Discussion in 'C++' started by GBoyle, Sep 11, 2010.

  1. GBoyle

    GBoyle New Member

    Joined:
    Sep 11, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  2. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    The error is due to the pointer BPrsn* .. you casted BPrsn* as (double *) and again recasted to BCorp* ...rite?? But i think that instead of (double *) u should use (void*) ....Please get back to me if i m correct..
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice