#Singleton Implamentation

1 messages · Page 1 of 1 (latest)

calm blade
#

I have copied this implementation of a class out of a book though i can't quite understand it. Would someone please explain to me what line 4 of the .cpp file does?

lucid wolf
calm blade
#

i understand that part, though this is the line that i can't wrap my head around Texture_Manager* Texture_Manager::m_s_instance = nullptr;

#

why is there Texture_Manager* data type before an already declared variable?

lucid wolf
#

That assert in the constructor makes sure that the program crashes when the constructor is called twice

calm blade
#

o

#

lol

calm blade
#

does it do anything outside of assigning the member variable a nullptr value?

lucid wolf
#

no

#

also i'm pretty sure it's a static variable

calm blade
#

oh lol, thank you

lucid wolf
#

not a member

#

so it's like a global

calm blade
#

is this like a static variable convention then?

#

where you need to specify the data type of the static var before assigning it it's first value?

lucid wolf
#

yes i think

calm blade
#

that makes a lot more sense then, thanks once again!

lucid wolf
#

it's why you need to do int MyClass::method(int a, int b)

#

ig

calm blade
#

ye but that makes sense with a method since it has return type

#

but var does not need return type

#

it's prolly some static shananaghas then

wraith relic
# lucid wolf not a member

m_s_instaanec is a member of the class. Only it's a static member, so there's one instance of it for the class, rather than one instance for each object of that class type.

storm pulsar
void drum
#
class TextureManager
{
public:
  static TextureManager& instance()
  {
    static TextureManager tm;
    return tm;
};

No mucking around with pointers.
No worries about lifetime.

wraith relic