🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #4055

methods for accessing attributes

#4055
From: Steve Clamage
Date: Tue, 16 Apr 1996 06:10
92 lines
3313 bytes
I'm not sure if this has been previously discuessed, but so
far I've seen no mention of it, so...

Has there been any consideration of adding to the standard
a way of overloading access to instance variables, so that
code that directly accesses a variable can be made to call 
an access function if the class is modified to need it?

For instance, let's say we've got:
	
	class Display {
	public:
		int wid, hgt;
	}

(with lots of other useful stuff too, but we'll omit that for now)

If lots of code already exists like the following:

	void blah(Display &d)
	{
		d.wid = 80;
	}

And now suppose that we want Display to do something when
the width is changed--- has there been any consideration of adding
a way of doing this?  (besides changing all the direct accesses
to function calls)   
Or is there some way, within the current standard, to do this?
I toyed with some ways that involved replacing the type of 'wid'
(int) with some class that did the desired operation when assigned to,
but in order to have full access to Display, it has to be a specialized
class for each instance variable, and each class has to have special
code containing knowledge of the offset of 'wid' within 'Display'
and then it has to use that offset along with some really evil
casting to find the address of the 'Display' object it resides in.
Or is there an easier way?

Or shall all old code of this sort be simply converted to use
access methods, and should directly accessible public 
instance variables be completely avoided?

If that's the case, then I'd like to stick with some sort of 
standard naming convention...  but I've seen all kinds of different
variations...

So, which of these do people think is the most acceptable?

	1)
		class Display {
			int wid;
		public:
			int get_wid();
			void set_wid(int);
		}

	2)
		class Display {
			int _wid;
		public:
			int wid();
			void wid(int);
		}

	3)
		class Display {
			int wid;
		public:
			int _wid();
			void _wid(int);
		}


(Are there others I'm not aware of?)

I've used all three methods at different times over the years.
Although, lately, it seems that #2 comes closest to resembling
the normal method of accessing the instance variables, and within
the class it makes it clear when a publicly (through
the access methods) available instance variable is being accessed.


Any comments, suggestions, preferred techniques, etc?




[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
Technical Details
Message-ID: <9604160147.AA25931@grace>
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage
From: clamage@taumet.eng.sun.com (Steve Clamage)
Newsgroups: comp.std.c++
Subject: methods for accessing attributes
Date: 16 Apr 1996 06:10:00 GMT
Organization: Rochester Institute of Technology, Rochester, NY
Approved: clamage@eng.sun.com (comp.std.c++)
Message-ID: <9604160147.AA25931@grace>
NNTP-Posting-Host: taumet.eng.sun.com
Content-Transfer-Encoding: 7BIT
Content-Length: 2570