This may not be a question specific to C++ and more to do with Object oriented programming. I am new to this and I am doubtful of my design. I have a class Parser that basically implements many functions dealing parsing expressions, conversion from infix to postfix etc. I use these Parser functions in the main function. I realized that I do not need any data members for this class. Hence, I do not really need an object of this class. Hence, I ended up making every function static in the class. Is there something strange about this design. Should I have this as an interface instead? Any suggestions?
-
8In general, in C++ one would place non-member functions inside a namespace. There is no need for a class to group functions like this. – juanchopanza Feb 14 '13 at 23:57
-
1If you only have one Parser, a namespace would do. If you have Parser1 and Parser2 and want to be able to use either one, this design is a sensible way to do it. – Marc Glisse Feb 15 '13 at 00:00
-
Why not two different namespaces in that case? – user592748 Feb 15 '13 at 00:02
-
1Related to http://stackoverflow.com/questions/7345956/advantages-of-classes-with-only-static-methods-in-c – amdn Feb 15 '13 at 00:02
-
1@user592748 you can't pass a namespace as a template argument. – Marc Glisse Feb 15 '13 at 01:41
4 Answers
You want a parser and you know what you want it to do for you - this is in effect, your "interface".
Your current implementation of the parser doesn't need any member variables - therefore, to implement your interface, you don't need a class. So yes, do away with your static methods. Like Kevin says, using a namespace with plain old functions (non-static) is a great idea.
If you feel you will need to add a new parser that WILL need to maintain internal state, then you probably want to define an interface in (1) - a plain old publicly visible header file with function declarations inside a namespace of your choice is enough.

- 43,122
- 10
- 80
- 104
-
Thanks. That clears it up. Thumbs up for clarifying the meaning of interface and explaining how to deal with the 2nd case. – user592748 Feb 15 '13 at 00:30
-
Static methods can't be called from the constructor of a parent object automatically. – peterh Jun 15 '16 at 17:08
A class with nothing but static functions seems pretty indistinguishable from a namespace to me. So, why not just use a namespace?

- 182,031
- 33
- 381
- 347
-
Not sure how vital this is, but you can't mock a namespace (in the mocking frameworks that I know of). – Oct 02 '13 at 22:07
-
1
-
1A constructor isn't interesting because that's no different than just another static function. A destructor might be interesting, but an interesting destructor probably needs at least one boolean data member to record whether the class has been moved (and therefore whether the destructor shouldn't do whatever interesting thing it was going to do). – Lily Ballard Jun 17 '16 at 01:29
-
1No, because static functions can't be declared as members of parent classes, while data-less member classes can. Thus the constructor of a member-less class in an enclosing class will be called automatically. This feature can'be reached by a static function. – peterh Jun 17 '16 at 01:57
The way to decide for this question is on how will the functions be used?
1) If all the functions are used in one file and do not need to be exported anywhere, then definitely use static functions. Why? Because you can just type them directly into the body of the class in the .cpp file and you do not have to worry about maintaining declarations and keeping parameters aligned. Because when a C++ class is parsed all the code inside each function defined inside the class body is skipped and then parsed once all the classes members have been declared, so the functions can all see each other and are in a better name situation.The compiler will also inline a lot of the smaller functions if you declare them directly in the class like that.
2) If the functions need to be used from outside the current .cpp file, then use normal functions. Because later they can be used from anywhere else and exporting them by name is easier.
It is common to make utility functions static, so, if the functions of your Parser class do not rely on each other, you totally can made them static. If they rely on each other, and it may be possible that the same functions can be done another way, you should consider to use an interface

- 2,940
- 5
- 34
- 57