Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to do with private member functions when turning static class to namespace in C++?

I have a class that has 5 static public functions and 1 static private function (called from one of the public functions). The class doesn't have any member variables. It seems to me that it should be a namespace and not a class. But what to do with the private function? I prefer it not to be accessible by every namespace user, but there is no access control in namespaces.

like image 826
Igor Avatar asked Jan 17 '10 12:01

Igor


People also ask

Can static member functions be private?

Making a function a static member of a class rather than a free function gives two advantages: It gives the function access to private and protected members of any object of the class, if the object is static or is passed to the function; It associates the function with the class in a similar way to a namespace.

Can a namespace be private?

A process can create a private namespace using the CreatePrivateNamespace function.

Can we declare a static function as virtual?

Can Static Functions Be Virtual in C++? In C++, a static member function of a class cannot be virtual. Virtual functions are invoked when you have a pointer or reference to an instance of a class. Static functions aren't tied to the instance of a class but they are tied to the class.

Should free functions be static C++?

Declaring free functions as static gives them internal linkage, which allows the compiler more aggressive optimizations, as it is now guaranteed that nobody outside the TU can see that function.


3 Answers

There are two ways i know of

Don't declare them in the header

One way is to not declare those functions inside the header. They can be placed into unnamed namespaces within the implementation file, only.

Indeed, you will then have to implement any function that accesses this private function in the implementation file (not inline in the header).

Put them into a detail namespace

Preferably, you put them in a different header, and include them. So the code of them won't disturb your interface header. This is how boost does it, too:

#include "detail/destroy.hpp"

namespace orbit {
  void destroy() {
    detail::destroy_planets();
    detail::destroy_stars();
  }
}
like image 176
Johannes Schaub - litb Avatar answered Oct 07 '22 14:10

Johannes Schaub - litb


I don't think there is a solution to this :)

One -different- solution, would be to separate these functions into a separate compilation unit, then declare the private functions inside an anonymous namespace.

like image 32
Khaled Alshaya Avatar answered Oct 07 '22 14:10

Khaled Alshaya


Keep the public declaration in the header file. Move the implementations to a cpp file. Mark previously private methods as static. This will make them unaccessible from a different linker objects (compilation units) and effectively hide them.

like image 27
Marcin Seredynski Avatar answered Oct 07 '22 14:10

Marcin Seredynski