In many cases, I believe that my classes need private functions in order to destroy their functionality and code reuse. Typical implementation:
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 fine, 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 for internal purposes only and is not needed outside of MyClass.
if many private functions are required, we risk polluting the header file with unnecessary private functions that make the code less clear, increase compilation time and more difficult to maintain.
I know about the Pimpl idiom that solves all these problems, but my question is whether we want to use Pimpl, is it normal to do something like this for several 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, you do not need to include AnotherClass.h in MyClass.h, and fooPrivate () cannot be called by anyone other than from MyClass.cpp and after its declaration. I'm right?
Are there any caveats using this or will I have problems with my program?
source share