Thread: Accesor Methods in Games

  1. #1
    I am he who is the man! Stan100's Avatar
    Join Date
    Sep 2002
    Posts
    361

    Accesor Methods in Games

    Suppose I have a class

    Code:
    class Cat
    {
    public:
    Cat(){};
    ~Cat(){};
    int age;
    };
    Should I make an accesor method for int age? Or in any class. Is plainly calling it faster even the tiniest bit? It is a pain to make
    int age private, then create an accesor method, and a set method, and possbily an addto(int add) method.
    Stan The Man. Beatles fan

    When I was a child,
    I spoke as a child,
    I thought as a child,
    I reasoned as a child.
    When I became a man,
    I put childish ways behind me"
    (the holy bible, Paul, in his first letter to the Cor. 13:11)

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    If your class turns out to have very little data in it, and you do relatively little with that data, it might be best not to bother with a class at all. You could use a struct, but generally I would only use a struct within a class anyways.

    If you have a lot of data, then you most likely wont need to have simple functions like "addto()"...However, if you have a complex class, even if you do relatively little with a data member, it is best to make it private. You can always name the function age() anyhow.

    For an example, the std::string class must have a member that contains its length, so the accessor function is named length()
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    The only difference (in C++) between a class and a struct is that a struct is public by default and a class is private by default.

    However, by convention, structs are usually used for small amounts of data, and classes are usually used for larger objects.

    for example:

    Code:
    struct POINT
    {
    int x, y, z;
    };
    
    class WINDOW
    {
    ...a bunch of functions for Windows...
    };

    In your case it would be better to just access the variable directly and not use a function.

    It would save a lot of stack space and call time to just reference the variable directly instead of having a function just return it.

    However, when you make a large class, although it would still be more efficient to reference the variable directly, the convention is to use functions to modify and access it.
    My Website

    "Circular logic is good because it is."

  4. #4
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    If you are using a class, I'm assuming you're looking at some semblance of Object Oriented Design. If that is the case, you'd want to make an accessor function.

    If you decide now that the cat's age doesn't matter, then you could just set it anywhere with

    Code:
    Cat felix;
    felix.age = 99;
    However, as you code, if you decide that you need to limit the age of the cat to a range, say 0..25 (an extreeeeemly old cat!), you would have to go back through the code that uses your Cat class and make changes everywhere you used felix.age. If instead, you made age private, you would have to have been setting/retrieving it with

    Code:
    felix.getAge();
    felix.setAge(99);
    So when you wanted to limit setting the age to that range, you only change the setAge() function to either throw an exception, make that call to setAge(99) set the cat's age to 25 instead, or just ignore the request (depends on what makes the most sense to you).

    If you are looking for the nth degree of speed, consider this:

    Code:
    class Cat {
      private:
        int age;
      public:
        Cat(){};
        ~Cat(){};
        int getAge() { return age; }
        void setAge(int new_age) {
          if ((new_age >= 0) && (new_age <= 25))
            age = new_age;
          return;
        }
    }; // Cat class
    The compiler would inline the getAge() and setAge() functions, so you would not lose any speed as functions defined in the class declaration are inlined by default (unless the compiler decides they are too big, too complex or something else that would make inlining it actually slower). What you would gain however would be the ability to modify how that age is set by changing only one function rather than having to search through code to find any place you used felix.age for something.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    In general, real designs don't tend to be bogged down with get/set pairs, mainly because you tend to do more complicated things with your methods.

    Also, it decreases efficiency, slightly, but it greatly reduces coupling -- you want your classes to be as independent of each other as possible, and accessor methods help achieve this.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. When done right, PC games are amazing
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-13-2008, 05:32 PM
  2. Violent video games?
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 58
    Last Post: 04-26-2006, 01:43 PM
  3. Hooked on old games...... still
    By Stoned_Coder in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 05-30-2005, 02:46 PM
  4. Video Games Industry. 5 years left.
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 12-10-2002, 10:52 PM