Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class with no data members in C++

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?

like image 579
user592748 Avatar asked Feb 14 '13 23:02

user592748


People also ask

Can a class have no data members?

No, 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.

Can class have only member functions with no data members?

Explanation: Class definition must end with a semicolon, not colon. Class can have only member functions in its body with no data members.

Which function is not a member of a class?

1 Answer. Friend function is not a member of the class.

What is a class Datamember?

A data member may be of any type, including classes already defined, pointers to objects of any type, or even references to objects of any type. Data members may be private or public, but are usually held private so that values may only be changed at the discretion of the class function members.


1 Answers

  1. You want a parser and you know what you want it to do for you - this is in effect, your "interface".

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

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

like image 77
Carl Avatar answered Sep 27 '22 23:09

Carl