Setting And Accessing Private Member Variables

This is a discussion on Setting And Accessing Private Member Variables within the C++ Programming forums, part of the General Programming Boards category; In classes, my book uses examples like Code: GetAge(int itsage) const {return itsage} /*and*/ SetAge(int age) {itsage = int age} ...

  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,317
    >> 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,670
    another common idiom:

    Code:
    struct person
    {
    	int
    	age(void) const
    	{
    		return age_;
    	}
    
    	void
    	age(int set)
    	{
    		age_ = set;
    	}	
    
    	protected:
    	
    	int age_;
    };



    ITSA
    Socket Library!

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

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