🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by Frank Barrus Mon, 15 Apr 1996 16:36
methods for accessing attributes
#4041
Author: Frank Barrus
Date: Mon, 15 Apr 1996 16:36
95 lines
3562 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?


-- 
Frank "Shaggy" Barrus: feb6399@osfmail.isc.rit.edu, shaggy@csh.rit.edu
http://www.csh.rit.edu/~shaggy
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]
Re: methods for accessing attributes
#4061
Author: Andrew Bell
Date: Tue, 16 Apr 1996 20:13
47 lines
2249 bytes
feb6399@osfmail.isc.rit.edu (Frank Barrus) wrote:
>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?

As a general rule, directly addressable public data is to be avoided,
for a variety of reasons.

>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...

Let me put in my two cents worth:

A verb in a member function name should indicate that the function is
not const, and vice versa.  It is, after all, useful to know which
you're working with at any given time.  Thus for property foo, which
may or may not be an explicit member of class Bar, we have:

class Bar
{
	public:
		int foo() const;
		void setFoo(int);
			or
		void set_foo(int)
	private:
		int myFoo;
};

I like the "my" prefix for non-static member variables, as you can
see.

Note that underscores preceding a name are often reserved by the
system.  While underscore followed by a lowercase letter is not
reserved, it is probably better to avoid using it anyway.

Andrew Bell
abell@mindspring.com
 
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu 
]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads