Results 1 to 4 of 4

Thread: Class and Inheritance problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2002
    Posts
    24

    Class and Inheritance problem

    I have a base class with about 5 classes that inherit the one base class. I also have an array of about 1000 base classes that store the class when I create it.

    Baseclass BaseClasses[1024];

    When I want to store the variable and use it I do this:

    BaseClasses[m_iLoc] = storevariable;
    Baseclass *pVar = &BaseEnts[m_iLoc];
    ClassCar *pVar2 = (ClassCar *)pVar2;
    pVar2->Go();

    Yet when I try to get some data from the BaseClasses it seems that some of the data is wrong or corrupt. I think this is because the BaseClasses variable is Baseclass only and not one of the other classes. What can I do to fix this?

  2. #2
    Junior Member
    Join Date
    Nov 2002
    Location
    Newcastle, Australia
    Posts
    20
    Your pVar2 is not initialised to anything. You should have
    ClassCar* pVar2 = (ClassCar*)pVar;
    pVar2->Go();

    Thats if I'm understanding what you are actually after
    Last edited by gommo; Feb 4th, 2003 at 04:32 PM.

  3. #3
    Fanatic Member McCain's Avatar
    Join Date
    Jan 2002
    Location
    Sweden/Denmark
    Posts
    802
    Your pVar2 is not initialised to anything. You should have
    ClassCar* pVar2 = (ClassCar*)pVar1;
    pVar2->Go();
    What's pVar1? Shouldn't it just be pVar?
    Never argue with fools, they will only drag you down to their level, and beat you with experience.

    Q: How do you tell an experienced hacker from a novice?
    A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Your objects are of the base class type. You may not cast them to derived class types.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width