Thread: get/set

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    get/set

    I was wondering, why do people use a public get/set function inside a class in order to get/set a private variable instead of declaring the variable public in the first place? I don't doubt the usefulness of doing so since it is more than common, but I would like some example showing the importance...

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Encapsulation.
    If you at any point in the future want to change how you set/get those variables, you're future proof with set/get (you don't need to change the client code that calls the class functions, only the class functions themselves).
    Otherwise you'll have to change the underlying code that calls the class to match the new implementation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    71
    Quote Originally Posted by Elysia View Post
    Encapsulation.
    If you at any point in the future want to change how you set/get those variables, you're future proof with set/get (you don't need to change the client code that calls the class functions, only the class functions themselves).
    Otherwise you'll have to change the underlying code that calls the class to match the new implementation.
    hmmmm but do you actually practice this in real life as well?
    Just curious....

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What does "practice this in real life" mean? Or rather, what do you mean?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    71
    Quote Originally Posted by Elysia View Post
    What does "practice this in real life" mean? Or rather, what do you mean?
    I hope I don't sound sarcastic just now but what I meant is that when you write a real C++ program,not toy code,do you use the set/get cause I rarely heard anyone in C++ using get/set although it's a norm in java programming.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I see.
    Although I don't work in the programming industry, when I write code, I use a "special" get/set. It's a template class that works like get/set, but you can still use the variable as if it was not get/set.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Another side of this is validation and conversion, for example, let's say we have a graphics library that supports setting pixels to R, G, B colours, and we want to support 24 bpp and 16 bpp (5,6,5)

    We'd first make a base class for pixel colour, which is just there to define the interface. We derive from this class to make all the REAL implementations [this is just a trivial sample - a real example of how to do this would require quite a bit more code, and could probably be done partially using templates]
    Code:
    class BaseColor
    {
       public:
          BaseColor();
          virtual ~BaseColor();
          virtual SetColor(int r, int g, int b) = 0;
          virtual GetColor(int &r, int &g, int &b) = 0;
    };
    
    class Color24BPP: public BaseColor
    {
    private: 
       BYTE m_r;
       BYTE m_g;
       BYTE m_b;
    public:
         Color24BPP();
         ~Color24BPP();
         virtual SetColor(int r, int g, int b) 
         {
             // Clamp colours in 0..255 range.   We assume the number is positive. 
             m_r = min(255, r);
             m_g = min(255, g);
             m_b = min(255, b);
         }
         virtual GetColor(int &r, int &g, int &b) 
         {
             r = m_r;
             g = m_g;
             b = m_b;
         }
    };
    
    
    class Color16BPP: public BaseColor
    {
    private: 
       BYTE m_r;
       BYTE m_g;
       BYTE m_b;
    public:
         Color16BPP();
         ~Color16BPP();
         virtual SetColor(int r, int g, int b) 
         {
             // Clamp colours to 5, 6, 5 bits.   We assume the number is positive. 
             m_r = min(31, r >> 3);
             m_g = min(63, g >> 2);
             m_b = min(31, b >> 3);
         }
         virtual GetColor(int &r, int &g, int &b) 
         {
             // Make it back into 8 bit numbers. Or in the upper bits in the bottom to make 
             // some data in the bottom bits. 
             r = m_r << 3 | m_r >> 3;
             g = m_g << 2 | m_g >> 4;
             b = m_b << 3 | m_b >> 3;
         }
    };
    --
    Mats
    Last edited by matsp; 09-26-2008 at 09:24 AM. Reason: Fix bug.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Jul 2008
    Posts
    71
    hmmm can you show me your special set/get.I learn c++ but i stopped until template ,the basic. part.
    Just curious how you implement your performance just as without set/get but with safety just as with get/set

    just posted this and saw matsp post above.wow,it never cross my mind using set/get with polymorphism.I'm saving this page in my hard drive for future reference
    Last edited by kypronite; 09-26-2008 at 08:10 AM.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    This is the set/get class I use:
    Code:
    namespace Stuff
    {
    	template<typename Type, typename Class, typename ReturnType = Type> class CUtilityMember
    	{
    	private:
    		Type Data;
    		typedef const ReturnType& (Class::*GetFncPointer)();
    		typedef void (Class::*SetFncPointer)(const Type& NewData);
    		GetFncPointer m_pGet;
    		SetFncPointer m_pSet;
    		Class* m_pClass;
    	
    	public:
    		CUtilityMember(Class* pClass, GetFncPointer pGet = NULL, SetFncPointer pSet = NULL);
    		CUtilityMember(const Type& NewData, Class* pClass, GetFncPointer pGet = NULL, SetFncPointer pSet = NULL);
    		CUtilityMember& operator = (const Type& NewData);
    		operator Type () const;
    	};
    
    	template<typename Type, typename Class, typename ReturnType> CUtilityMember<Type, Class, ReturnType>::CUtilityMember
    		(Class* pClass, GetFncPointer pGet, SetFncPointer pSet): m_pClass(pClass), m_pGet(pGet), m_pSet(pSet) { }
    	template<typename Type, typename Class, typename ReturnType> CUtilityMember<Type, Class, ReturnType>::CUtilityMember
    		(const Type& NewData, Class* pClass, GetFncPointer pGet, SetFncPointer pSet): Data(NewData), m_pClass(pClass),
    		m_pGet(pGet), m_pSet(pSet) { }
    	template<typename Type, typename Class, typename ReturnType> CUtilityMember<Type, Class, ReturnType>& CUtilityMember
    		<Type, Class, ReturnType>::operator = (const Type& NewData)
    	{
    		if (m_pSet)
    			(m_pClass->*m_pSet)(NewData);
    		else
    			Data = NewData;
    		return *this;
    	}
    	template<typename Type, typename Class, typename ReturnType> CUtilityMember<Type, Class, ReturnType>::operator
    		Type () const
    	{
    		if (m_pGet)
    			return (m_pClass->*m_pGet)();
    		else
    			return Data;
    	}
    }
    Then I can do like this (see that they are added just like normal variables, but requires initialization):
    Code:
    	#pragma warning(push)
    	#pragma warning(disable: 4355)
    	class CFilter
    	{
    	public:
    		CFilter(Strings::CStringEx strExt = _T(""), Strings::CStringEx strDescription = _T("")): strExt(strExt, this),
    			strDescription(strDescription, this) { }
    		CUtilityMember<Strings::CStringEx, CFilter> strExt;
    		CUtilityMember<Strings::CStringEx, CFilter> strDescription;
    	};
    	#pragma warning(pop)
    And:
    Code:
    CFilter filter;
    filter.strExt = "*.mkv";
    filter.strDescription = "Matroska file";
    cout << "File extension is: " << filter.strExt << endl;
    cout << "File description is: " << filter.strDescription << endl;
    Obviously it has a small overhead, but nothing significant.
    (Some lines have been wrapped to avoid having to scroll.)
    Although I'm sure some of the class could be improved (less memory overhead, for example).
    Last edited by Elysia; 09-26-2008 at 08:29 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jul 2008
    Posts
    71
    ^I have a hard time understanding your code,although it more to do with the template syntax. thanks anyway.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it is a bit advanced.
    You had better study templates a bit more to understand it fully.
    I'm also working on a revamped version of it that's more efficient (and also more complex).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    My get/set methods, when used, involve get and set member functions in my classes. I don't see the need for Elysia's overhead when most of the time a simple
    Code:
    class thing{
    private:
      int t;
    public:
      int get(){return t;}
    };
    will suffice.
    Also, mats. Your GetColor functions are a little buggy for both children.
    Last edited by twomers; 09-26-2008 at 09:29 AM. Reason: spelling

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by twomers View Post
    Also, mats. Your GetColor functions are a little buggy for both children.
    Yes, fixed the "set b three times instead of setting r, g, b" (there may be other bugs= - I did just hack that up as an example to show the get/set that does different things for different variants of derived classes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by twomers View Post
    I don't see the need for Elysia's overhead when most of the time a simple will suffice.
    Well, everyone works in different ways and like different things
    This is my way of doing it, though. It's my way to find the "in-between" of get/set and no get/set.
    We all agree that get/set is good.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    better:
    Code:
    class thing{
    private:
      int t;
    public:
      int get() const {return t;}
    };

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. private/public get/set
    By George2 in forum C# Programming
    Replies: 2
    Last Post: 05-04-2008, 12:49 AM
  2. Trivial: Get/Set + Function Order in Classes
    By Ashes999 in forum C++ Programming
    Replies: 10
    Last Post: 07-08-2003, 11:09 AM
  3. Elevator program
    By brianptodd in forum C++ Programming
    Replies: 14
    Last Post: 11-01-2002, 11:42 PM