Class or Struct?

This is a discussion on Class or Struct? within the Game Programming forums, part of the General Programming Boards category; Hi I am making a pretty large text game and I now have to make the player file. I am ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Class or Struct?

    Hi I am making a pretty large text game and I now have to make the player file.
    I am torn betwen either this:

    Code:
    struct Player
    {
        static string m_nm;
        static int m_hp;
    };
    
    string m_nm = "";
    int m_hp = 100;
    which looks like I am attempting to find a way round of not global variables

    or this way: ps - this a basic example of what I might do

    Code:
    class Player
    {
    public:
        Player();
        ~Player();
    
    protected:
    // pointers
        string *m_nm;
        int *m_hp;
    };
    
    Player::Player()
    {
        m_nm = new string("");
        m_hp = new int(100);
    }
    
    Player::~Player()
    {
        delete m_nm;
        m_nm = NULL;
        delete m_hp;
        m_hp = NULL;
    }
    
    int main()
    {
        Player *play = new Player;
    
        delete play;
        play = NULL;
    
       return 0;
    }
    Basicly, I was thinking of doing the second method, all the Player data members
    woulds be derived from a base class.

    Which method is best to use?

  2. #2
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I would go with the class method every time.
    Oh and you might find this macro useful:

    Code:
    #define SAFE_DELETE( p ) \
    { \
        if ( p ) \
        { \
            delete p; \
            p = NULL; \
        } \
    }
    Note that I would line up the '\' vertically but it's a pain on this forum.

    EDIT: I only use struct's for smaller collections of data, like messages. Of course, use whatever you prefer.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,553
    How is that any safer than delete p; p = NULL;
    deleteing NULL is harmless, and it won't protect you from non-NULL pointers which shouldn't be deleted.

    Furthermore, what are you going to do when you should be using delete [ ] p;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    74
    Class is the way to go because you will have functions inside the Player class to manipulate its data members. Although I would lay off of the pointers and the obsession with dynamic memory allocation where it isn't needed you are inviting unnecessary memory leak issues, etc.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,469
    http://www.parashift.com/c++-faq-lit...s.html#faq-7.8

    A struct has its members public by default, whereas a class has them private. If you don't come from a C background this difference may seem unimportant. Especially since you can still move from a struct to a class and viceversa by simply adjusting the access labels. So... who cares? Right?

    Well, you do Think of the difference between both and use that as the basis for your decision. If you want to build an object class that is mainly composed of public access members (functions and data), use a struct. Otherwise use a class. You can still do the other way around, nothing stopping you. The compiler will not care as long as the access labels are correctly placed. But... you and all other humans reading your code will more readily understand it.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by Salem
    How is that any safer than delete p; p = NULL;
    deleteing NULL is harmless, and it won't protect you from non-NULL pointers which shouldn't be deleted.

    Furthermore, what are you going to do when you should be using delete [ ] p;
    It's not "safer", sorry I worded it wrong. Convenient is the word.

    I define two macros, SAFE_DELETE and SAFE_DELETE_ARRAY.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    How is that any safer than delete p; p = NULL;
    deleteing NULL is harmless, and it won't protect you from non-NULL pointers which shouldn't be deleted.

    Furthermore, what are you going to do when you should be using delete [ ] p;
    This is if and only if the compiler you are using follows the standard. MSVC 2005 tends to be much better than MSVC 6, but will often times still fire off an exception when attempting to delete a NULL pointer. Perhaps they left it in there as a pre-cautionary measure to help the programmer find obvious errors.

    SAFE_DELETE, SAFE_DELETE_ARRAY and SAFE_RELEASE have been in just about every game programming book I've purchased in one form or another. It would be interesting to see what www.gamasutra.com has to say about the issue.

    For now, I'll use them because if anything it is a fore-warning to me that I'm not doing something right in my code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. const elements of struct / class not accessible?
    By talz13 in forum C# Programming
    Replies: 2
    Last Post: 03-24-2006, 04:05 PM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 07:18 AM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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