Joy
Joy

Reputation: 1787

creating methods that doesn't depend on a object

How can I create bunch of methods that doesn't depend on any object ( in my file there is no classes , no objects , no main , nothing but the methods) all in one cpp/hpp file and how to declare them ?

Upvotes: 1

Views: 99

Answers (6)

JaredPar
JaredPar

Reputation: 755239

To define a function that doesn't depend on an object simply declare them directly.

// MyFile.h
int some_function();

// MyFile.cpp

int some_function() {
  return 42;
}

Using C++ though it would be a good idea to declare them in a namespace though. This doesn't give them a dependcy on an object but does reduce global namespace pollution

// MyFile.h
namespace MyNamespace {
  int some_function();
}

// MyFile.cpp
using MyNamespace;
int some_function() {
  return 42;
}

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258618

If you want them to be public, i.e. available in multiple translation units, the best way is to include them in a namespace, declare them in the header and implement them in a single implementation file:

//functions.h

namespace MyFunctions
{
   void foo();
   void goo();
}

//functions.cpp

namespace MyFunctions
{
   void foo() {}
   void goo() {}
}

IMPORTANT

If you provide the definition in the header, you should mark them inline, otherwise including that header in multiple translation units might result in a linker error:

//functions.h
inline void foo() {  
    //..
}
inline void goo() { 
    //..
}

If you only want them available in a translation unit, define them in that implementation file in an anonymous namespace (or declare them static):

//fileThatUsesFunctions.cpp

namespace
{
    void foo() {}
    void goo() {}
}

Upvotes: 2

Christopher Berman
Christopher Berman

Reputation: 739

You should place them in a namespace. Naveen's example is spot on.

As an additional note, if you wish to hide certain functions or data units within the namespace (thereby mimicking 'private' access), place those functions and data units in an anonymous namespace, nested within the parent namespace. For example:

namespace Foo
{
    publicFunction1();
    publicFunction2();

    namespace
    {
        privateFunction1();
        std::vector<Bar> privateData;
    }
}

Items within a nested, anonymous namespace are only accessible to the items within the parent namespace. I've found this to be singularly useful.

Upvotes: 1

P.P
P.P

Reputation: 121407

Nothing stops you from writing free functions. If you think that a function should be global then free functions are quite appropriate.

Upvotes: 1

linuxuser27
linuxuser27

Reputation: 7353

You declare them the same way you would in C. It can be within a namespace or outside of a namespace. There is no difference other than the fact that they are not in a class.

If you want to use the functions in C later you should prepend them with extern "C".

extern "C" void foo();

Upvotes: 1

Naveen
Naveen

Reputation: 73483

Create a namespace. Something like this:

Utils.h

namespace Utils
{
   void myMethod();
}

Utils.cpp

namespace Utils
{
  void myMethod()
  {
    //Implementation
  }
}

Upvotes: 4

Related Questions