When I was first working with classes it allways annoyed me typing out "Get..." and "Set..." at the start of Private member access functions. The "Get../Set.." methods seem to be the major method of member access, but I've now use my own way, which seems to me a lot simpler than the "Get../Set.." system.
For member acces I use two overloaded functions of the name of the member, one with an arument of what it is to be set to, and one with no arument to get the value.
EG:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class Point {
public:
int X() {return mX;};
int X(int value) {return (mX = value);};
int Y() {return mY;};
int Y(int value) {return (mY = value);};
private:
int mX, mY;
}
//useage
Point myPoint;
//set x to 5
myPoint.X(5)
//get x and present it
cout << "myPoint.X : " << myPoint.X() << endl;
This removes the need to continuously type Get..() and Set..()
Sorry PseudoCode but you would not be able to patent your way - as it
is a fairly common way of getting/setting properties (for example the stringstream
class uses it);
Using Get and Set just involves a bit more typing, that's all, and quite frankly I think that it a more descriptive way of doing it.
If you look through 'professional' class libraries you will see that it is used one hell of a lot (possibly Set moreso than Get).
Another quite common one are the is ones, which are used for boolean properties - For example: if (button.isChecked() )if (window.isVisible() )if (file.isOpen() )
"Out of curiosity: Why do you return an int from the "setter" versions? "
A better question would be why not. In a lot of cases it's usefull to combine multiple steps into one when setting, for instance...
1 2 3
if (!(hWnd = CreateWindow(...))) {
//error code
}
rather than:
1 2 3 4
hWnd = CreateWindow(...)
if (!hWnd) {
}
And it's not at a loss of efficiency since there is no return code compilled (well, it actualy depends on your compiller settings) unless you're actualy using the returned value.
"Sorry PseudoCode but you would not be able to patent your way "
I certainly wasn't thinking of that, I was just offering an alternative to Get and Set, since that's the way I see most people doing it, especialy beginners like I did at first, but not all of whom are inventive enough to come up with thier own styles like me.