There can only be one belief.
The Singleton mode is as literal as it means: only one instance can be created. At first glance, think about how it is possible to create countless instances as long as it is a class. Let's take a look at how God makes himself unique.
① God can create everything, and everything cannot create God. The method is to hide the construction method of "God ".
Class God
{
PRIVATE:
God ();
~ God ();
}
② Who created God? He can only be himself now.
Class God
{
PRIVATE:
Static god * _ godinstance;
Protected:
God ();
~ God ();
Public:
Static god * godinstance ();
}
God * God: _ godinstance = 0;
God * God: godinstance ()
{
_ Godinstance = new god ();
Return _ godinstance;
}
③ The constructor is hidden to prevent god from being freely created by others.
Class God
{
PRIVATE:
Static god * _ godinstance = 0; // use static to make the Variable _ godinstance initial
// Only executed during the first class instantiation
PRIVATE:
God ();
~ God ();
Public:
Static god * godinstance ();
}
God * God: _ godinstance = 0;
God * God: godinstance ()
{
If (godinstance = 0)
{
_ Godinstance = new god ();
}
Return _ godinstance;
}
④ Believers can only declare the existence of God.
Int * Main (INT argc, char * argv [])
{
God * SGN = God: godinstance ();
}