0

I am learning c++ from a java background. I would like to know how I can create a private function in a namespace?

I would like to do this in cpp

Java:

public class Clazz { public static void foo() { // stuff } private static void bar() { // other stuff } } 

C++:

namespace clazz { void foo(); // what do I do here? } 
9
  • 1
    Who should be able to call the namespace's private function? Commented Dec 22, 2014 at 3:16
  • Only foo should be able to have access to the private function. Commented Dec 22, 2014 at 3:17
  • Those two examples aren't equivalent. The first is in class scope, the other namespace scope. Commented Dec 22, 2014 at 3:17
  • Just do what you've done in java, instead of having a naemspace called clazz just have a class, with private static member functions. Commented Dec 22, 2014 at 3:18
  • I don't want to have a class in c++. From my understanding, there's no reason that this needs to be a class if I only want utility functions. Am I mistaken? Commented Dec 22, 2014 at 3:18

5 Answers 5

1

There are no private functions in namespaces in C++. Generally people use a namespace specifically for implementation-specific details:

namespace clazz { namespace detail { void bar(); } void foo(); } 
Sign up to request clarification or add additional context in comments.

3 Comments

How do I declare/use bar?
@user1811367 From within foo() you would call it like detail::bar();. To define bar() you would just do namespace detail { void bar() { /* ... */ } } (assuming you were already in namespace clazz).
bar still has external linkage in this example
1

If you would like to have limit the access of a non-instance method, you'll have to make it a static method on a class. E.g. in C++:

class clazz { public: static void foo(); private: static void bar(); }; 

It's worth mentioning that if you only use bar in a single .cpp file, you use an anonymous namespace to prevent it from being accessed elsewhere. So you have something like:

clazz.h:

namespace clazz { void foo(); } 

clazz.cpp:

#include "clazz.h" namespace { // bar can only be used in this file void bar() { /* bar implementation */ } } void clazz::foo() { /* foo implementation */ } 

Comments

1

Coming from Java, you may not realize that in C++ the implementation is often in a separate file (.cpp, .cc) from the declaration of the class (.h, .hpp). The best thing to do with utility functions that only need to exist in one cpp file is to place them in an anonymous namespace.

namespace { void my_utility_function(const Something &something) { // bunch of code } } // close namespace 

This function is visible anywhere in this cpp file forward, but not elsewhere. So the class methods that need my_utility_function are implemented below here.

When a function is in a named namespace, the linker is going to emit data for it and waste time looking for usage elsewhere in the program. Even if you avoid name conflicts (by using namespace), it's a waste of CPU. This also documents that it is basically private. In C, a one labeled a function static with the same effect, but static has many different meanings and AFAIK the anonymous namespace is preferable style.

You can nest the anonymous namespace inside a named namespace, in case the entire cpp file is living in a namespace.

Comments

0

In a C++ class the member function is private by default.

You can state that explicitly by using an access specifier, like this:

private: 

The effect lasts until the next access specifier in the class, or the end of the class definition.

One difference from Java is that C++ private member functions are not final. When they're virtual they can be overridden in derived classes. In C++ member functions meant to be overridden are often private.


C++ namespaces do not have accessibility.

In effect they're all public.


The Boost library uses a convention where a nested namespace named detail is to be regarded as an implementation detail, as if it were private.

A more direct naming could be impl or implementation.

Such conventions are enough to prevent inadvertent access of and dependence on implementation details.

Comments

0

The "C++ way" to solve this problem is to use a namespace as you have done, and give bar internal linkage.

clazz::bar() will be callable from anywhere in this source file, not just from foo(). Keeping it inside namespace clazz protects against Murphy, not Machiavelli. If this visibility is a genuine problem then you could either:

  • separate your code into more source files so that this file only contains foo and bar.
  • use the class with only static member functions, as a hack

The simplest syntax for internal linkage is:

static void bar() { } 

although you can use an anonymous namespace instead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.