 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
john smith Guest
|
Posted: Sat Sep 10, 2005 12:06 am Post subject: static variables in a class |
|
|
I was looking at some code that someone had written, and they had a class
with all static attributes.
Can someone please enlighten me why someone would do that? Doesn't that
break the rule of encapsulation?
I have always been used to seeing accessors and mutators instead of directly
accessing an attribute to change or
access the variable.
Thanks in advance for any insight.
|
|
| Back to top |
|
 |
Shezan Baig Guest
|
Posted: Sat Sep 10, 2005 12:15 am Post subject: Re: static variables in a class |
|
|
john smith wrote:
| Quote: | I was looking at some code that someone had written, and they had a class
with all static attributes.
Can someone please enlighten me why someone would do that? Doesn't that
break the rule of encapsulation?
I have always been used to seeing accessors and mutators instead of directly
accessing an attribute to change or
access the variable.
Thanks in advance for any insight.
|
Probably just for scoping reasons. Alternatively, they could have just
used a namespace, but maybe there was some reason to prevent it being
re-opened?
Hope this helps,
-shez-
|
|
| Back to top |
|
 |
Dave Rahardja Guest
|
Posted: Sat Sep 10, 2005 1:41 am Post subject: Re: static variables in a class |
|
|
On 9 Sep 2005 17:15:08 -0700, "Shezan Baig" <shezanbaig2004 (AT) gmail (DOT) com> wrote:
| Quote: | john smith wrote:
I was looking at some code that someone had written, and they had a class
with all static attributes.
Can someone please enlighten me why someone would do that? Doesn't that
break the rule of encapsulation?
I have always been used to seeing accessors and mutators instead of directly
accessing an attribute to change or
access the variable.
Thanks in advance for any insight.
Probably just for scoping reasons. Alternatively, they could have just
used a namespace, but maybe there was some reason to prevent it being
re-opened?
|
Right. I've done that to get around an old compiler that has unreliable
namespace support.
By the way, John, how do static variables break the rule of encapsulation?
The use of accessors and mutators is a good idea, but there are times that you
want to let other entities (especially other classes belonging to a common
library) access your members directly for efficiency or even clarity.
|
|
| Back to top |
|
 |
M Guest
|
Posted: Sat Sep 10, 2005 6:39 am Post subject: Re: static variables in a class |
|
|
It's difficult to comment on that specific code without seeing it, but
most of the cases of static members that I've seen or used were simply
to make the same data accessible to every instance of the class such
as an instance counter or a lookup table.
On Fri, 9 Sep 2005 17:06:31 -0700, "john smith"
<princetonharvard (AT) charter (DOT) net> wrote:
| Quote: | I was looking at some code that someone had written, and they had a class
with all static attributes.
Can someone please enlighten me why someone would do that? Doesn't that
break the rule of encapsulation?
I have always been used to seeing accessors and mutators instead of directly
accessing an attribute to change or
access the variable.
Thanks in advance for any insight.
|
|
|
| Back to top |
|
 |
Marcus Kwok Guest
|
Posted: Tue Sep 27, 2005 5:52 pm Post subject: Re: static variables in a class |
|
|
Dave Rahardja <ask (AT) me (DOT) com> wrote:
| Quote: | The use of accessors and mutators is a good idea, but there are times that you
want to let other entities (especially other classes belonging to a common
library) access your members directly for efficiency or even clarity.
|
I found these articles on why getters/setters (also called
accessors/modifiers and inspectors/mutators) should be avoided. It is
given in a Java context but the principles apply to OO design in C++.
Why getter and setter methods are evil
http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html
More on getters and setters
http://www.javaworld.com/javaworld/jw-01-2004/jw-0102-toolbox.html
--
Marcus Kwok
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|