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?
There is no such thing as a static class in C++. The closest approximation is a class that only contains static data members and static methods. Static data members in a class are shared by all the class objects as there is only one copy of them in the memory, regardless of the number of objects of the class.
A function that is declared static using the ' static ' keyword becomes a static function in C++. When a function inside a class is declared as static, it can be accessed outside the class using the class name and scope resolution operator ( :: ), without creating any object.
You can't override static methods. You can overload them (which is what you're doing here), but you're just getting static dispatch.
And while some modern languages do support static constructors for precisely this purpose, C++ is unfortunately not one of them.
Actually, not only is it okay, I would actually recommend it.
private
functions may be used, and sometimes must (when accessing private elements) however there is one issue with them: even if it is only a declaration, they clutter the class definition: a user of the class should not have to care or be exposed to the class internals.
On the other hand, static
functions or functions declared in an anonymous namespace within a source file are "free". No matter how many of them you have:
If there is one downside, though, it is that on those same Itanium-related tool-chains their absence of name make for poor backtraces without debug symbols. It can be seen as a minor inconvenience though.
Note: not having access directly to private
members of the class is rarely an issue because a method of the class can easily pass a reference to those members to them. It does mean they cannot build an instance of the class when the constructors are not public however.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With