i see sometimes this constructor writing with inline explicit. for example:
protected :
inline explicit Singleton() {
CCASSERT(Singleton::instance_ == 0, "error Singleton::instance_ == 0.");
Singleton::instance_ = static_cast<T*>(this);
}
inline ~Singleton() {
Singleton::instance_ = 0;
}
for what inline explicit is good for ?
inline
is necessary if you define a function in a header, but not in a class definition. It allows the function to be defined in multiple translation units (i.e. when including the header from multiple source files). The purpose is to allow the compiler to inline calls to the function - many compilers require the definition to be available within the translation unit in order to do that.
In this case it's pointless: functions defined within a class definition are implicitly inline.
explicit
means that the constructor can't be used for implicit type conversions. Historically, it only made sense for single-argument constructors; but I think these days it can also be used to prevent brace-initialisation.
In this case, it's also pointless: default constructors aren't used for implicit conversions.
for what inline explicit is good for ?
Here, they both give useful code smells - the author values verbiage and excessive structure over clarity. This is further evidenced by the use of the Singleton anti-pattern - tread carefully in this code.
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