we can achieve singleton without that also |
No, you need line 7. single IS the pointer to the single instance of Singleton.
This can get a little confusing. The Singleton class is just normal class. What gives it its singleton properties is the static member pointer (single) and the fact that getInstance() always returns the same value (the "real" instance created at line 27).
Note that lines 45 and 47 both call getInstance(). The first time getInstance() is called, it allocates a new instance of Singleton and assigns it to the static pointer single. Any subsequent calls to getInstance() will not allocate a new instance and will return the existing static value of single. Since the member pointer single is static, there is only one instance across all instances of Singleton.
Since Singleton's only constructor is private, it is not possible to construct an instance of Singleton directly. If you want to access something in the Singleton class, you must get a pointer to it via a call to getInstance() (which returns the pointer at line 7).
Keep in mind that this is a very simplistic example. This Singleton class doesn't have anything in it besides the logic to create / return a pointer to a single instance of the class. In practice, you would add attributes and/or functions to Singleton, or to a class derived from it (preferred).
To reiterate what
iHutch105 said, you need to think carefully about why you need a singleton class. They are often misused. Perhaps all you need is a static member of an existing class.