static variables in a class

J

john smith

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.
 
S

Shezan Baig

john said:
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-
 
D

Dave Rahardja

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.
 
M

M

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.
 
M

Marcus Kwok

Dave Rahardja said:
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
 

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,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top