Thread: Making a human class...not sure what to do

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by maxorator
    Why have a gender as functions setGender() and getGender()? Can't you just have a public gender variable?
    Sure you can. But the moment you want to store the gender in some different way, or log a message whenever the gender is changed (because laws against cross-gender operations have been passed, for example) or want to enforce some restrictions, you're pretty much screwed with a variable. Getters/setters are future-oriented programming, and thanks to inlining, they're usually free in terms of runtime performance.

    Also gender is a good thing to be hold in a BOOL data type.
    No, it's not. First, nothing is a good thing to hold in a BOOL data type, because that's a Win32 "C has no bool" type. If anything, use a proper C++ bool.
    It's also conceptually wrong. is_male could be boolean, or is_female. gender cannot be boolean - what does true mean? Make's me think of Austin Powers.
    Form: Sex?
    Austin: Yes, please!
    An enum is by far the best choice for gender. It's an enumeration of a finite and small set of possible values, namely "male" and "female".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Quote Originally Posted by CornedBee
    It's also conceptually wrong. is_male could be boolean, or is_female. gender cannot be boolean - what does true mean? Make's me think of Austin Powers.


    An enum is by far the best choice for gender. It's an enumeration of a finite and small set of possible values, namely "male" and "female".
    A bool would be plain stupid in that case. enum is the best way to go.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM