tags:

views:

203

answers:

4

I have a C++ class which contains only static data members. I noticed the compiler is OK if I define the access methods as const, as static, or as "regular" - so all seem to work.

My question is what is the correct/better practice in this case?

Thanks!

+2  A: 

Static accessors for static data members.

Seth Illgard
My compiler does not seem to allow having a method both static and const. I believe that once a method is defined static, then by design it cannot modify the class non-static members - since this method does not "belong" to a specific instance of the class.
Mym
You are right, my fault.
Seth Illgard
Thank you. This makes sense to me.
Mym
@Mym: In your question you state that the class contains only static data members. If the class also contains non-static data, please edit the question. This answer is correct to your question: If you only have static data members, the most appropriate solution is using only static methods on them.
David Rodríguez - dribeas
For now there are only static data members. The thing is that I want to maintain the flexibility to enhance the class in the future (potentially using local data members) without the need to update any calling code.This is why I want the calling code to create an instance of this class and use the methods via this instance. This is why I was thinking not to define them as static.
Mym
If it is just statics now, it should not be a class now. If you want to change that later, you can always refactor later. If you force users to create objects even thought they don't hold any state, then that's just confusing.
sbi
At this point you have a design (whether explicit or just thought of) of your system. When you bind operations together you are assigning a name to it, and that name has a meaning for you and for others to read. At this point you should be able to discuss whether X (whatever your name is) is instantiable.
David Rodríguez - dribeas
Don't forget about thread safety in case you need to use your static data members in a multi threaded environment.
fritzone
+2  A: 

If your class contains only static members, you should probably be using the singleton pattern.

Alex Deem
I agree, such a class is effectively a singleton.But what is the problem if I keep it like this?
Mym
But... be careful. The singleton pattern is often overused.
rlbond
Actually, there are very few "real" cases where you need the singleton patter, I believe that a class with only static functions is perfectly ok.
Seth Illgard
@Mym: The problem is that you cannot control the order or initialization of static data members. With a singleton you can use different approaches that guarantee the proper initialization (before use) in all scenarios, while with your solution if another static object (global or static data member of other class) access this class' static members during construction then there is no guarantee that this class members have been properly initialized.
David Rodríguez - dribeas
Thanks for this data!
Mym
@dribeas: another static object in a different compilation unit, that is.
Steve Jessop
Consider the "Borg" pattern: http://code.activestate.com/recipes/66531/ (that article is Python-based, but the pattern is not. The C++ equivalent would be to have non-static member functions which act on static member variables). Alex Martelli points out that the important thing is usually the uniqueness of the state. But Singleton is defined by the uniqueness of the object, which usually is irrelevant. I think of Borg like handles - a handle on a kernel object is not unique, whether the resource it represents is unique or not. Making the handle a singleton would be an unnecessary constraint.
Steve Jessop
A: 

If all methods are static then there is no point in creating an instance of the class. So I suggest to make all methods static so that you can use those methods without creating the object of the class. Basically, you will be able to call the methods using the namespace syntax.

Naveen
True, so calling code can either create an instance of the class (no penalty, empty c'tor, no non-static data members), or by directly accessing the static methods without creating an instance.
Mym
Yes..but creating an object directly looks a bit odd for all static variable class.
Naveen
@Mym: If the class is not meant to be instantiated, you should hide the constructors. Your interface should offer only those operations that make sense for your design. If there are only static data and methods, the constructor should be private (and unimplemented).
David Rodríguez - dribeas
+2  A: 

I have a C++ class which contains only static data members.

Then that probably shouldn't be a class, but either free functions in a namespace or a singleton.

sbi
I still find it easier to encapsulate them in a designated class since it has a specific role and I have quite a few methods which manipulate these static data members (three different maps).
Mym
+1 concur! You only need a header wich declares some functions. These functions will mutate whatever static storage variable you desire. No need to declare them in a class and make other translation units depend on this class declaration.
sellibitze
So, a translation unit should be your "abstraction" (not a class) and a header file would be the interface that doesn't contain any implementation details.
sellibitze
@Mym: A class is the template for objects. Objects combine state (i.e. data) with behavior (methods). If you have a class that doesn't have state and that isn't there for instantiating objects, then you do _not_ have a class, but a collection of algorithms. Some newer OO languages force you to assemble those in classes, but in C++ you don't need to. You might want to read this article: http://www.idinews.com/quasiClass.pdf
sbi
Copying the comment I've written in another answer:For now there are only static data members. The thing is that I want to maintain the flexibility to enhance the class in the future (potentially using local data members) without the need to update any calling code. This is why I want the calling code to create an instance of this class and use the methods via this instance. This is why I was thinking not to define them as static.This is why I still want to maintain a class here.
Mym
@Mym: Why do you copy your comment from there to here? Shall I now copy my answer from there to here, too?
sbi