473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Accessor Methods

Can someone explain what these really are for example:

----------------------------------------------------------

void SetFrameRate(int iFrameRate) { m_iFrameDelay = 1000 / iFrameRate; };

----------------------------------------------------------

I know what functions are and i know what void is and stuff but i dont get
the whole `Accessor Methods` term i guess.

Regards,
Carl
Jul 22 '05 #1
3 4526
LuCk wrote:
Can someone explain what these really are for example:

----------------------------------------------------------

void SetFrameRate(int iFrameRate) { m_iFrameDelay = 1000 / iFrameRate; };

----------------------------------------------------------

I know what functions are and i know what void is and stuff but i dont get
the whole `Accessor Methods` term i guess.

Here's what

http://cplus.about.com/library/gloss...ssormethod.htm

has to say:

An accessor method is a method used to examine or modify the members
of a class. Variables declared as private as accessed indirectly via
these methods.

Also Known As: accessor

Examples:

class Square {
private:
int side;
public:
void set_side(int length) {side = length};
int get_side() const;
int calc_area() const;
};

In the above example, set_side and get_side are accessor methods.

I would add that there are often good reasons to use accessors, rather
than making data public. The accessors give you a chance to make sure
data are valid before actually modifying the object, as well as a chance
to log changes.

Suppose, in the above example, that "side" takes on a value you do not
intend. You can validate changes to the variable, and log failures,
from the accessor:

void set_side( int length )
{
if( length > 0 )
side = length;
else
std::cerr << "warning: can't set_side( " << length << " )\n";
};

Hth,
Jeff

Jul 22 '05 #2
"LuCk" <Lu**@insight.rr.com> wrote...
Can someone explain what these really are for example:

----------------------------------------------------------

void SetFrameRate(int iFrameRate) { m_iFrameDelay = 1000 / iFrameRate; };

----------------------------------------------------------

I know what functions are and i know what void is and stuff but i dont get
the whole `Accessor Methods` term i guess.


The term has nothing really with the language per se. It stems from
the notion of a "property" or a "trait" of a particular "object" in
Object-Oriented terminology.

It is said that objects may exhibit certain quantifiable traits, which
in programming can be represented by member functions called "accessors".
Those traits can be implemented as values stored directly or calculated
using some kind of specific to the object algorithm.

In your example, the "frame rate" characteristic is _set_ by the function
but in the implementation we can see that the "frame rate" is not stored
directly but rather "frame delay" is calculated and stored.

The usual way to name those functions is "set..." or "get..." where the
ellipsis represent the trait being set or obtained. For example, class
Car could have a characteristic called "Speed", and the accessor
functions for it would be "setSpeed" and "getSpeed". However, it would
probably not have "speed" as a stored value, but rather the current gear
engaged and the current degree of the gas pedal depression, which
influences how fast the car is going. The implementation of "getSpeed"
would query the "speedometer" [sub]object to obtain the reading, and the
implementation of "setSpeed" would depend on the current speed at which
the car is moving to direct some force to either the gas pedal or to the
brake pedal while the reading of the speedometer is observed until the
speed reaches the required value. That's called "algorithm". However,
to the observer of the car it needn't be known. The observer just calls
"setSpeed" with a value and expects the car to respond somehow.

Well, you probably would benefit from a decent book on OOD. Ask in the
comp.object newsgroup what they recommend for beginners.

Victor
Jul 22 '05 #3
LuCk wrote:
Can someone explain what these really are for example:

----------------------------------------------------------

void SetFrameRate(int iFrameRate) { m_iFrameDelay = 1000 /
iFrameRate; };

----------------------------------------------------------

I know what functions are and i know what void is and stuff but
i dont get the whole `Accessor Methods` term i guess.

Regards,
Carl


Accessors decouple the interface of a class from the way its objects
store their state. In your example, the class which setFrameRate
belongs to communicates with the outside world in terms of frame rate
but actually stores another quantity (that is presumably more useful
to other members). We can also update multiple member variables or
check for invalid parameters in a setter function.

Going a step further, a class attribute needn't be backed by a member
variable at all:

#include <os_stuff.h>

class File {
public:
File(char* filename)
{ handle_ = os_openFile(filename); }

// all the usual file operations...

date getDate()
{ return os_getFileDate(handle_); }

private:
os_Handle handle_;
};

All names are made up, there's no error
handling, etc., but you get the idea.
Have a good New Year's Eve,
Martin
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: jerrygarciuh | last post by:
Hello, I have been working for some time now on a PHP OOP database abstraction layer. Yes I know there are others out there which are maturing but I like reinventing this wheel. The task I...
22
by: mirandacascade | last post by:
When I look at how classes are set up in other languages (e.g. C++), I often observe the following patterns: 1) for each data member, the class will have an accessor member function (a...
2
by: Todd A. Anderson | last post by:
I've inherited two "C++" code bases that I have to tie together and both of them make frequest use of public member variables in spite of all the information saying this limits flexibility. Well,...
22
by: Generic Usenet Account | last post by:
A lot has been said in this newsgroup regarding the "evil" set/get accessor methods. Arthur Riel, (of Vanguard Training), in his class, "Heuristis for O-O Analysis & Design", says that there is...
6
by: Jason Shohet | last post by:
I have a class with protected variables and some accessor methods, , get, set ... Maybe I have a brain blockage today but I'm thinking, why not just make those variables public. After all,...
7
by: Tenacious | last post by:
I have been programming using C# for over a year now and I still wonder what is the importance of using accessor methods when the property is read-write. It seems easier to just make it a public...
8
by: AAJ | last post by:
Hi all I would like to have a class that can set/return values of different datatype via a single accessor, i.e. overload the accessor i.e. something like DateTime m_DateValue; string...
8
by: Tim Sprout | last post by:
Why is it considerd best practice to use Properties rather than Get and Set accessor methods? -Tim Sprout
2
by: rbjorkquist | last post by:
This is my first attempt at writing/using web services, so any and all comments will be greatly appreciated. With that said, I am also by no means saying this is the correct either. I have...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.