feelfree
feelfree

Reputation: 11753

Is there a need to set up a class for one or several functions in C++

I feel confused with whether there is a need to set up a class for one or several functions. I give the following example to make my point clear:

file1.h
   void Fun1();
   void Fun2();

file1.cpp
   void Fun1() {}
   void Fun2() {}

As you can see we have two functions here, and people using these functions just need to include the header file and then call them. Then, I also have the choice of setting up a class without any member variables insider but only for these two functions (suppose these two functions are closely related):

file1.h
   class Operation
   {
      Operation() {};
      ~Operation() {};
      void Fun1();
      void Fun2();

    };
file1.cpp
    void Operation::Fun1() {};
    void Operation::Fun2() {};

Then my question is which practice is better and why. Thanks.

Upvotes: 2

Views: 92

Answers (4)

CounterTorque
CounterTorque

Reputation: 7

Since you asked specifically about C++, the most correct answer would be to namespace them:

file1.h:

namespace MyNamespace
{
    void Fun1();
    void Fun2();
}

file1.cpp:

namespace MyNamespace
{
    void Fun1() {}
    void Fun2() {}
}

Then to use them you would simply call MyNamespace::Fun1() etc.

As I started out saying, this is most correct for C++.

If you were writing this for C, or mixed C/C++ it should be noted that C does not support namespaces. So in that case making them static functions of a class would be a better organizational route.

file1.h

class MyClass
{
public:
    static void Fun1();
    static void Fun2();
};

file1.c

void MyClass::Fun1() { }
void MyClass::Fun2() { }

NOTE: if you are doing mixed programming you should consider making it easier to include, but that's beyond the scope of this question.

Upvotes: 1

Ivan Gusev
Ivan Gusev

Reputation: 93

There should be a reason you want to put them in one class. If it's just for grouping, namespace better suits for this role.

file1.h
   namespace Operation
   {
      void Fun1();
      void Fun2();
    };
file1.cpp
   namespace Operation
   {
      void Fun1(){};
      void Fun2(){};
   };

P.S. If for some reason you would still prefer to use class, make functions static at least, so you wouldn't need to create an instance of this class.

file1.h
   class Operation
   {  
   public:    
      static void Fun1();
      static void Fun2();
    };
file1.cpp
    void Operation::Fun1() {};
    void Operation::Fun2() {};

Upvotes: 2

adrin
adrin

Reputation: 4896

You don't "have to" put them in a class, you can go for both implementations. It's mostly a design preference. Java coders are used to putting those functions as static functions of a utility class, but in C++ you don't have to, although some people do.

If your concern is just encapsulation of those functions in a specific context, you might also consider putting them in namespaces.

Upvotes: 2

Stephane Rolland
Stephane Rolland

Reputation: 39906

You should use a dedicated namespace

file1.h:

namespace MyDedicatedNameSpace
{
   void Fun1();
   void Fun2();
}

file1.cpp:

void MyDedicatedNameSpace::Fun1() {}
void MyDedicatedNameSpace::Fun2() {}

Or if you want to use a class, you should set these functions as static:

file1.h:

   class Operation
   {
public:
      static void Fun1();
      static void Fun2();
    };

file1.cpp:

void Operation::Fun1() {};
void Operation::Fun2() {};

Upvotes: 2

Related Questions