In many cases I find my classes needing private functions to break down their functionality and reuse code. The typical implementation would be :
MyClass.h
#include "AnotherClass.h"
class MyClass {
public:
float foo() const;
private:
float fooPrivate(const AnotherClass& ac) const;
}
MyClass.cpp
#include "MyClass.h"
float MyClass::foo() const {
return fooPrivate(AnotherClass());
}
float MyClass::fooPrivate(const AnotherClass& ac) const {
return ac.foo();
}
This is ok, however declaring fooPrivate() in the header file can be problematic in the following cases :
we might not want to include AnotherClass in the header file if it is only for internal uses and is not needed outside MyClass.
if many private functions are needed we risk polluting the header file with unnecessary private functions that make code less clear, increase compile time and is more difficult to maintain.
I am aware of the Pimpl idiom that solves all these problems but my question is if we do not want to use the Pimpl is it ok to do something like this for a few functions ?
MyClass.h
class MyClass {
public:
float foo() const;
}
MyClass.cpp
#include "MyClass.h"
#include "AnotherClass.h"
static float fooPrivate(const AnotherClass& ac) {
return ac.foo();
}
float MyClass::foo() const {
return fooPrivate(AnotherClass());
}
In this case it is not needed to include AnotherClass.h in MyClass.h and fooPrivate() can not be called by anyone except from inside MyClass.cpp and after it has been declared. Am I right?
Are there any caveats using this or will I end up with problems when my program gets bigger?