2

i saw that some times in c++ applications using only namespace declarations with header and source file like this :

#ifndef _UT_
#define _UT_

#include <string>
#include <windows.h>

namespace UT 
{
    void setRootPath(char* program_path, char* file_path);
    char * ConvertStringToCharP(std::string str);
};

#endif

//and then in UT.cpp
#include "UT.h"

namespace UT 
{
    char * ConvertStringToCharP(std::string str)
    {
        char * writable = new char[str.size() + 1];
        std::copy(str.begin(), str.end(), writable);
        writable[str.size()] = '\0';  
        return writable;
    }

    void setRootPath(char* program_path, char* file_path) 
    {
        //...
    }
}

is it better then defining classic class with static methods?
or just simple class ?
dose this method has something better for the compiler linker ?

the methods in this namespace are called allot of times .

Griwes
  • 8,805
  • 2
  • 43
  • 70
user63898
  • 29,839
  • 85
  • 272
  • 514

6 Answers6

5

Performance-wise, there's no difference between having static class members and free functions in a namespace. It's a matter of logic though. Are your functions related to the class or not?

A good question to ask yourself - are you creating the static member functions inside the class just for better organization (or just so you can group them together)? If the answer is yes, you should probably use a namespace.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
5

By shear coincidence I happen to read this answer for a slightly different question in Stack Overflow. In that the user rhalbersma had given a nice link to a Dr Dobb's article where the author Scott Meyers explains how methods implemented outside the class (non-friend methods), but inside the same namespace actually improve encapsulation. For me it was a good learning for today. Hope this helps you as well.

Community
  • 1
  • 1
PermanentGuest
  • 5,213
  • 2
  • 27
  • 36
4

You put a method outside all classes when the method's meaning is independent of a class. Static classes in other languages (Java, C#) are a way to compensate for inability to put methods outside classes. Since C++ provides this ability out of the box through namespaces, the use of an additional "static class" would be counterintuitive to the readers of your code.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

The primary reasons for using a static class member function is a logical and conceptual relation to the class and its members.

Another reason may be to enable template partial specialisation, which is not allowed for function templates, but for classes.

Otherwise, use a standalone function (defined in an appropriate namespace).

Walter
  • 44,150
  • 20
  • 113
  • 196
  • -1 You can provide specializations for free template functions. If this isn't what you meant, rephrase your answer. If it is and I understood correctly, you're giving false information here. – Luchian Grigore Jun 11 '12 at 14:11
0

By default you should always try to minimise the methods on a class. If you can implement functionality in terms of the class rather than within a class you should. In other words if you can achieve the desired functionality through using the already published public interface then you should. This vastly reduces the dependency of your code on implementation details.

So if you can then implement it outside of the class in an appropriate namespace that naturally seems related to the class.

EDIT: It appears that the OP is really asking about whether to implement a namespace or a static class as a holder object for utility/related functions. In my opinion namespaces are the correct way to do this, because that is what they are there for. A class is not necessary.

Dennis
  • 3,683
  • 1
  • 21
  • 43
  • this answer doesn't _quite_ address the question – Mooing Duck Jun 11 '12 at 14:56
  • @MooingDuck - In what way? The OP's question was whether it was better to implement it on the class or not. My answer states that it is generally better to implement a function outside of the class if possible. – Dennis Jun 11 '12 at 15:31
  • The question is about if functions should be in a namespace or should they be static members of a static function. It's a given that the functionality can be done outside the class. – Mooing Duck Jun 11 '12 at 16:01
  • I thought that the OP was asking about static member functions. However it seems you are right about it, and OP wanted clarification about whether to implement a separate class with static functions or a namespace function. However same issues then apply to other answers to this question in that case. See the answers of Luchian, Unni and Walter. I will modify my answer now, thanks for pointing that out. :-) – Dennis Jun 11 '12 at 16:26
0

There's no absolute rule, but in general, unless the functions show a very large degree of coherence, and you want to close the "namespace" for some reason, so clients can't add functions to it, using namespace is generally preferable to using class. Unless, of course, the intent is to make them available to templates: you can instantiate a template on a class, but not on a namespace. (This technique is often called "traits"; a class with no non-static members is called a traits class.)

Even if you do use namespaces, you should define the functions:

void UT::setRootPath( char const* programPath, char const* filePath)
{
    //  ...
}

char* UT::convertStringToCharP( std::string const& str )
{
    //  ...
}

This way, any typos in the signature of the function will be detected by the compiler.

James Kanze
  • 150,581
  • 18
  • 184
  • 329