Re: Using Get() and Set() instead of accessing the variable directly
I agree with the Get() statement being public.
I suppose I just make it either private or protected so it is not accessible
and shows that it is not needed by other classes.
The const statement must be when using references or pointers?
Thanks for the info. It makes sense and is good practice!
I guess I never use the const statement just because, I just know that I am
using it.
If I need to modify something, I usually create a statement like.
void GetX(int* x){ *x = m_x; }or void GetX(int& x)( x = m_x; }
instead of
int* GetX(){ return &m_x; } or int& GetX(){ return m_x; }
I see a lot of Microsoft code do it that way, so I just follow their
example.
It makes sense and is a coding style that I prefer.
That way, there is no confusion as to what the return type should be.
Set() should not be plublic.... and accessible
only by internal, or by a friend object... like
a container object (or a list object..)
I guess that all depends on how the variable is being used.
It could be either private, public or protected.
If it is private to the class, then it is private.
If it can be modified by subclasses, then it should be protected.
If it can be modified by other classes, then it should be public.
That would be application specific.
Something like an Addition class would need to be declared public for
example.
Addition.SetX(1);
Addition.SetY(2);
Addition.Add();
Ans = Addition.GetResult();
"jmarc" <jmarc@incursion-voyages.com> wrote in message
news:D3_Lh.112455$Du6.75270@edtnps82...
As long as we talk about data objects...
Get() should be defined as public and const...!
Set() should not be plublic.... and accessible
only by internal, or by a friend object... like
a container object (or a list object..)
This means, clients can work with a 'const' data object
in order to avoid non allowed modifications to this
jmarc...
"Tom Serface" <tom.nospam@camaswood.com> wrote in message
news:OZGGwczaHHA.4552@TK2MSFTNGP05.phx.gbl...
Just like OOP is better than the top down method. Nice list.
Tom
"Nobody" <Nobody@yahoo.com> wrote in message
news:uaNaD7xaHHA.4832@TK2MSFTNGP02.phx.gbl...
Yeah. It is hard to get the notion of assignment -vs- Get() and Set()
methods to be equivalent.
I not only think it is cleaner, it should be standard practice.
I have been burned in the past by directly accessing the variable, then
later on breaking the class up
into relevant parts and then going back and change all the direct
variable access to Get() and Set() methods.
Plus, it is confusing when one class might access them with Get and Set
methods and another accesses them directly.
It makes it easier to search and change.
It saves time in the short run, but takes more time in the long run.
Just enforcing my beliefs.
1. Cleaner
2. Decouple Semantics from the Representation.
3. Easier if the class is broken up into relevant subclasses.
4. Easier to seach.
5. Easier to make changes.
6. Really should be standard practice. At least for me it is.
It is a good habit to get into. IMO.