Article View: comp.std.c++
Article #4041methods for accessing attributes
From: Frank Barrus
Date: Mon, 15 Apr 1996 16:36
Date: Mon, 15 Apr 1996 16:36
95 lines
3562 bytes
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
]
Technical Details
Message-ID:
<4koq35$dcd@news.isc.rit.edu>Path:
arc1-ca.pugleaf.net!fido.asd.sgi.com!austernShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern From: feb6399@osfmail.isc.rit.edu (Frank Barrus) Newsgroups: comp.std.c++ Subject: methods for accessing attributes Date: 15 Apr 1996 09:36:12 PDT Organization: Rochester Institute of Technology, Rochester, NY Approved: austern@isolde.mti.sgi.com Message-ID: <4koq35$dcd@news.isc.rit.edu> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: 13 Apr 1996 18:00:05 GMT X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMXJ6/Uy4NqrwXLNJAQGWygH7BQVUAjIgKlP83xn37Bis/Rb5tvAea67P JI9pKhwUFhgiPXv3H6emVMwFiECW6ybQ1zlD096TH4IleODFChQJeg== =ynK9 Originator: austern@isolde.mti.sgi.com