C++, do private functions really need to be in the header file?

C++StandardsConventions

C++ Problem Overview


I have always thought of header files as a sort of 'public interface' describing a class, in which case it would be better to keep private fields and functions in the cpp file.

I understand that private fields need to be in the header so that other classes can tell how much memory an instance of a class will consume, but it occurred to me as I was about to write a private helper function, that this function could be made static, in which case there was no need for it to be 'part of the class' at all, it could just as easily be a regular function in the class definition's .cpp file.

It then occurred to me that all private functions could potentially be re-written to be static by accepting pointers/references to class fields instead of expecting to be defined in the class.

This would eliminate the need to declare any private functions in the header file.

I do like to follow conventions so now I want to ask, is it considered an established convention in C++, that non-static private functions should be in the header file? What about static functions or static constants?

EDIT: I'm going to put in some code to explain what I'm getting at:

.h file:

#ifndef SOME_CLASS_H
#define SOME_CLASS_H

class SomeClass
{
private:
	int x;
public:
	void combineWithX(int y);
};

#endif

.cpp file

#include "SomeClass.h"

void someHelper(int* x)
{
	*x = (*x) + 1;
}

void SomeClass::combineWithX(int y)
{
	someHelper(&x);
	x += y;
}

Note that someHelper(int* x) in the cpp file references the private member x in spirit, but not directly, and therefore does not need to appear in the header. I'm wondering if this sort of thing is considered 'bad style'

C++ Solutions


Solution 1 - C++

Private helper functions can be hidden from public header file by moving them to an inner class. This works because inner class is considered part of the class and can access the surrounding class's private members.

Unlike PImpl idiom, this does not have any dynamic allocation or indirection penalty. Compile times should be faster when editing/refactoring private functions, as there is no need to recompile all the files including the public header.

Example:

public .h file

#ifndef SOME_CLASS_H
#define SOME_CLASS_H

class SomeClass
{
private:
    // Just forward declaring in public header.
    struct Private;
    int x;
public:
    void combineWithX(int y);
};

#endif

in .cpp file

#include "SomeClass.h"

// Declare all private member functions of SomeClass here
struct SomeClass::Private
{
  static void someHelper(SomeClass& self)
  {
    self.x = self.x + 1;
  }
};

void SomeClass::combineWithX(int y)
{
    Private::someHelper(*this);
    x += y;
}

Solution 2 - C++

I agree that it is a problem that implementation details need to be exposed in a header file; it interferes with separation of interface and implementation.

Moving private helper functions to be free functions in the .cpp file (I presume that is what you meant by "static") won't work if those functions need to access private member variables.

You may be interested to look at the pImpl idiom (more)

Solution 3 - C++

How about

// MyClass.h -

MyClass {
public:
   // public stuff..

#ifdef mynamespace_myclass_p
private:
   // private stuff..
#endif // mynamespace_myclass_p

}

// MyClass.cpp - 

#define mynamespace_myclass_p
#include "MyClass.h"

// implementation code..

Eh, but then your headers are still cluttered with private stuff.

Headers are such a pain. I might be wrong, but at the moment I sort of feel that headers are the thing with the biggest pain / usefulness ratio in all of C++. You basically have to maintain all your method signatures in at least two places when you program in C++, and if you don't want your public headers cluttered with private stuff, you pretty much have to have 2 versions of your headers, which means maintaining your signatures in at least 3 places! Plus any forward declares that turn out to be needed. Seriously, they're so much more trouble than they're worth.

I'm really looking forward to seeing how C++20's module system plays out. It could be a huge boon. We've been waiting for it for almost 5 decades.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGhost314View Question on Stackoverflow
Solution 1 - C++balkiView Answer on Stackoverflow
Solution 2 - C++M.MView Answer on Stackoverflow
Solution 3 - C++ShavaisView Answer on Stackoverflow