Grammar in system/core/include/utils/Singleton.h

18 views
Skip to first unread message

yf...@mobvoi.com

unread,
Mar 11, 2016, 10:41:10 AM3/11/16
to android-platform
Hi:
    I'm not a c++ noobie, but grammar in system/core/include/utils/Singleton.h confused me a lot.

    For instance: this line:

        template<> TYPE* Singleton< TYPE >::sInstance(0);     

    I believe this grammar is a template class member declaration, but
        1. why there's a value, not a type in the parentheses ?
        2. there's no sInstance method in Singleton at all, there's a private member instead;

    So this is a variable declaration? Means sInstance is a pointer point to a TYPE instance, but
        1. why declare a pointer need a parameter?
        2. the sInstance is already declared in Singleton class, and it is private, what the purpose of the statement?

    What's more:
        This statement in frameworks/base/core/jni/android_hardware_SensorManager.cpp:
            SensorManager& mgr(SensorManager::getInstance());
        I found everywhere and not found parentheses operator override about (), somehow this statement did do a job like:
            SensorManager& mgr = SensorManager::getInstance();

    Is all this some new c++ features or do I missed anything?

Glenn Kasten

unread,
Mar 11, 2016, 10:48:12 AM3/11/16
to android-platform
The (0) is not a template parameter.  It is the initializer for sInstance.
As sInstance is a pointer of type TYPE *, the initializer is a zero (but NULL or nullptr might be clearer).
As mentioned, it is an initializer not a parameter

Note: discussion of the generic C++ language is probably off-topic for this group,
so I recommend that follow-ups move to Stack Overflow
Reply all
Reply to author
Forward
0 new messages