Accessor Methods

L

LuCk

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
 
J

Jeff Schwab

LuCk said:
Can someone explain what these really are for example:


Here's what

http://cplus.about.com/library/glossary/bldef-accessormethod.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
 
V

Victor Bazarov

LuCk said:
Can someone explain what these really are for example:

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
 
M

Martin Eisenberg

LuCk said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,883
Members
47,414
Latest member
djangoframe

Latest Threads

Top