Thread: Setting And Accessing Private Member Variables

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    545

    Setting And Accessing Private Member Variables

    In classes, my book uses examples like
    Code:
    GetAge(int itsage)  const {return itsage} /*and*/ 
    SetAge(int age) {itsage = int age}
    Things like that.

    Why would you do this and not jsut combine the functions?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> GetAge(int itsage) const {return itsage}
    That should be:
    Code:
    int GetAge()  const {return itsage;}
    where it returns the member variable.

    >> Why would you do this and not jsut combine the functions?
    How would you combine the functions? How would you be able to tell the difference between codethat wanted to get the current value and code that wanted to change the current value?

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    545
    Ah good point I think it was just that the book always used them in a row so that there was really no point in having the two.

  4. #4
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    as a general design point, don't provide getter and setter function for private variables unless you really need it. As soon as you do that, you're essentially exposing them as part of the interface of the class.

    for example, in a game you might have a Player class, with a private int member representing player health. Instead of a SetHealth method, it's better to have Damage/Heal/Kill/whatever methods. That way if you decide that health should be a float (for some reason), you don't break the interface
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    another common idiom:

    Code:
    struct person
    {
    	int
    	age(void) const
    	{
    		return age_;
    	}
    
    	void
    	age(int set)
    	{
    		age_ = set;
    	}	
    
    	protected:
    	
    	int age_;
    };
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-09-2007, 12:58 AM
  2. setting up and also accessing classes
    By Stevo in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 11:09 PM