문제

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.

다른 팁

explicit with constructors stops them being used implicitly.

inline is a way to tell compilers (in ye-olden-days when we lacked RAM) that the function is something it'd want to inline. Of course, nowadays they ignore us, or at most humour us with "I'll take it under advisement" with the subtext of "Silly human, thinks I didn't know that already?" (in short, inline is ignored these days, the compiler is in a much better position than us to decide to inline).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top