0

I give the following codes to illustrate my question:

class Abc
{
  public:
       void do_something() {};

};

class Def
{
 public:
      static void do_something() {};

};

Both class Abc and Def have no variables to keep the status of the class. In this case, which way of defining function do_something is better? Making it as a static function is better?

feelfree
  • 11,175
  • 20
  • 96
  • 167

4 Answers4

5

No, you got that completely wrong. Whether there are any instance variables keeping state is an implementation detail that you shouldn't care about in the design of your interface.

You use static methods (elsewhere called "class methods", which is a much better word) if a method applies to the class, and not an instance of the class. You use instance methods if you want to create objects that are instances of the class.

Since you didn't even mention the criterion that you should use, nobody can tell you what is better in your case. On the other hand, if you are talking about "the status of the class" (which is a thing that people don't talk about very often) that seems to indicate that you should use either class methods or a singleton.

gnasher729
  • 51,477
  • 5
  • 75
  • 98
3

It fully depends on design of your class and purpose of these functions. For example, if you want to follow open-closed principle you may want to ask yourself the question:

"Do I want to allow extensions (i.e. derived classes) to extend behavior of these functions in such way that they will become stateful (i.e. depend on some some not static members added in those derived classes)?"

Depending on your design the answer may be "yes" or "no" (which means that these functions are stateless by their original purpose definition). Note if you will want to allow also dynamic polymorphism than you'll need not only leave these functions not static but also make them virtual.

So, to answer this question you first need to carefully design your class. Given design decisions the answer will became more concrete.

Community
  • 1
  • 1
mvidelgauz
  • 2,176
  • 1
  • 16
  • 23
1

If the only functionality you need is do_something(), without any other fields in the class, then the best way, IMO, is not having classes at all. simply define

void do_something() {...}

And it will be easier to call, just do_something() and that's that.

If the function needs to operate on memeber variables of the class, then it should be a normal member function, in which case you call it from an instance:

ABC a;
a.do_something()

Static methods, on the other hand, are a means of making your code more organized, in the specific case when you have a full functioning class with members variables and member functions that work on them, but you also want one or more functions that don't relate to an instance specific variables states at all, but relate to the class in a logical way. That is, functions you could just as well write without being attached to a class at all.

For example, if you need a function that creates a specific instance of your class, like using a specific combination of constructor arguments, which would otherwise be to cumbersome to call each time, you could write it as a stand alone function, that creates and returns an instance of the class by calling the constructor with the arguments combination. But it's more resonable to explicitely relate it to the class. In this case, you can make it a static member function.

Israel Unterman
  • 13,158
  • 4
  • 28
  • 35
-2

static functions are used for

  1. calling on the class itself (not instances of the class)
  2. spreading function visibility to the same translation unit

Answering your question, the 'Abs' case is more preferable.

  1. If the class had had internal non-static state variables, it wouldn't have been possible to access them for your static function. Static functions access only static variables.
  2. It also wouldn't have been possible to call normal non-static class functions from your static function.
  3. Impossible to make factory functions, etc. There is no way to make an instance using new calling private/protected constructor.
Pleeea
  • 362
  • 4
  • 12