Having a class, that contains private
member function, I would like to mark as inline
(to explicitely mark my intent of hint that particular function may be inlined), but don't expose their implementation to an API consumer. For simplicity, it might look like:
Airplane.h
:class Airplane
{
char manufacturer[80];
char mode[80];
// ...
public:
void autopilot_steer_left(int degree);
// ...
private:
// ...
inline bool validate_hydraulic_on_left_wing(); // secret, patent-based, etc.
};
Airplane.cpp
:#include "Airplane.h"
// ...
void Airplane::autopilot_steer_left(int degree)
{
// ...
}
Where should I put definition of Airplane::validate_hydraulic_on_left_wing
? I know that common place is Airplane.h
(possibly inside class, so it is inline
implicitely), but this contradicts with my intent.
You can use the keyword inline
in the .cpp file:
inline void Airplane::autopilot_steer_left(int degree)
{
// ...
}
Thus your implementation will be hidden in the .cpp file and inline
as well.
As to the benefits of doing so, however, that is debatable. Here is a good discussion relating to inline
performance:
Benefits of inline functions in C++?
If the function cannot be called from outside of a single translation unit (i.e. cpp file), then there is often no real sense in marking it inline.
From C++ perspective, inline modifier only allows function to be defined in several units. There is no more semantics behind inline keyword.
From perspective of real inlining of function calls (i.e. compiler optimization), it may only be useful if your compiler is configured to never inline functions that are not marked as inline (like /Ob1 for Visual C++). However, it is quite normal for a modern compiler to try to inline all functions it can. In this case the compiler does not even look whether you have marked the function as inline or not: any call within a single unit would be inlined if compiler thinks it makes code better.
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