Member Accessors....Why?

This is a discussion on Member Accessors....Why? within the C++ Programming forums, part of the General Programming Boards category; What's the point? Can't you just make all the class members public instead of going through all that crap?...

  1. #1
    Registered User DeanDemon's Avatar
    Join Date
    Nov 2002
    Posts
    37

    Thumbs down Member Accessors....Why?

    What's the point? Can't you just make all the class members public instead of going through all that crap?
    -Dean

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    25
    well part of the idea behind having memeber variables private, is so that are protected from being messed with. If they were public, then anyone could get in and mess with the variables. But by having them private, the only way to access or manipulate them is through pre-written member functions. It is the idea of "black box".

    Van

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You can't change them later if you make them public.

    As an illustrative example, let's say you have a square class, with an area member function. If you had this as a public member, you would need to always store the area as a member, but if you make it a function, then you are at liberty to either keep it as data and then have the the area function return it, or you are free to simply compute it from the length.

    Essentially, the more implementation you hide, the more implementation you can change. If you don't think of a program totally as it exists now, but rather how flexible it is to change in the future, things like accessors make a big difference.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    If you use member functions then it is impossible to do evil things like taking the address of the member variable etc.

    It's also easier to extend the code.

    Example:

    Code:
    class Object
    {
    public:
     ....
      double getNum();
      double setNum(double num);
    private:
     ....
      double num;
    };
    Now in Object version 2.0, the number stored should be a complex<double> instead. Now it is very easy to extend the code, without having to change the code that uses the class:

    Code:
    class Object
    {
    public:
     ....
     double getNum()
     {
        if (num.imag() != 0) throw std::exception("bad num");
        return num.real();
     }
     double setNum(double num);
    private:
     ....
      complex<double> num;
    };
    In other words: It is better.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  5. #5
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    One solution is to use this header.

    Use it if you like, but it is not extremely useful.
    Attached Files Attached Files
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

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. Replies: 2
    Last Post: 04-19-2008, 12:06 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM

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