Class Organization

This is a discussion on Class Organization within the Game Programming forums, part of the General Programming Boards category; I have a question regarding the way I want to organize my game. Right now, the game is pretty much ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    18

    Class Organization

    I have a question regarding the way I want to organize my game. Right now, the game is pretty much finisihed, but the code is pretty ugly. Here is one scenario that I would like help on, and then I can probably apply that to the rest.

    Lets say there is one main class, called Person. There are two sub classes that inherit from Person, called Gunner and Fighter. Pretty much as follows:

    Code:
    class Person
    {
    public:
         char Name[100];
         int x_pos;
         int y_pos;
    };
    
    class Gunner : public Person
    {
    public:
         int amount_of_bullets;
         // random other attributes
    };
    
    class Fighter : public Person
    {
    public:
         char weapon_type[100];
         // random other attributes
    };
    Everything is nice and dandy right now. Then we have a class called Army, and it holds a vector of Persons, like so:

    Code:
    class Army
    {
    public:
         vector<Person> m_arUnits;
    };
    Now, the problem I am having is this: If you declare things like this:

    Code:
    Fighter Herc;
    Gunner Guns;
    Army Phalanx;
    
    Phalanx.m_arUnits.push_back(Herc);
    Phalanx.m_arUnits.push_back(Guns);
    You only have access to the Person attributes when you iterate through the vector. I want to be able to access the other things like weapon_type and amount_of_bullets. So basically my question is: Is there anything I can store the information in so I can access specific data relative to the current class? If not, can anyone give me advice on how to set up this scenario so that it can be organized and efficient, and also has room for growth?

    Thanks for any help,

    Khelder

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    create a boolean variable in the Person class, but do not initialize it. Initialize it in the Gunner and Fighter classes.

    a 1 can stand for a Fighter, and a 0 can stand for a Gunner.

    Then, when you iterate, check that boolean variable (you should be able to check it since it was originally created in the Person class, even though it was not initialized there).

    If it is a 1, typecast into a Fighter, otherwise, typecast into a Gunner.

    Typecasting should allow you to access those fields you need to access that are specific to Fighter and Gunner.
    My Website

    "Circular logic is good because it is."

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    18
    I tried typecasting, like this:

    Code:
    cout << ((Fighter *)Phalanx.m_arUnits[0])->weapontype << endl;
    On the plus side, the Fighters attributes became accessible. On the other hand, the following two errors wont go away.

    1)
    error C2440: 'type cast' : cannot convert from 'std::allocator<_Ty>::value_type' to 'Fighter'
    with
    [
    _Ty=Person
    ]

    2)
    error C2227: left of '->weapontype' must point to class/struct/union

    The second error is because of the first, but how can I fix the first. I checked other projects where I typecast a lot, and I did it the way in my example above. Am I doing something wrong, or is it impossible to organize things the way i am trying to?


    Thanks as always for any help,

    Khelder

  4. #4
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    try this and see if it works, not sure if it will or not, but try it anyways:

    Code:
    cout << ((Fighter)Phalanx.m_arUnits[0])->weapontype << endl;
    My Website

    "Circular logic is good because it is."

  5. #5
    Registered User
    Join Date
    Dec 2003
    Posts
    18
    That gives the same error also. Thanks for trying though. I think what I am trying to do may not be possible. I may just need a different vector for each type.

    Khelder

  6. #6
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    Try making your base class abstract, and then define everything in the children classes.
    My Website

    "Circular logic is good because it is."

  7. #7
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    The vector should hold pointers, not objects.

    A good idea is to define a virtual function process() in the base class which is run once per frame and is defined differently for each different object.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    I think you may have a slicing problem.

    Change
    vector<Person> m_arUnits;
    to
    vector<Person*> m_arUnits

    Lets say that you have a class A and a class B that inherits from A.
    There is no way to cast an object of A into B. The only way do it is by using pointers.

    A *a;
    B b;

    a=&b;

    ((B*)a)->....
    Last edited by erikj; 12-31-2003 at 08:20 PM.

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    18
    What is the benefit to a vector holding pointers rather than objects?

    Ill see if I can organize it some way so that virtual functions can do what I want them to do.

    Thanks for the ideas,

    Khelder

  10. #10
    Registered User
    Join Date
    Dec 2003
    Posts
    18
    Excellent! Pointers and virtual functions fixed my problem. Thanks for the help!

    Khelder

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Specializing class
    By Elysia in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2008, 05:30 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 02:09 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21