Skip to main content
gamedev.net
🔒 Locked

Singleton help...

Started by Remnex Oct 8, 2001 at 4:44 AM 2 replies 500+ views
Original Post
Remnex
Remnex
I was wondering if someone could point me to a place that contains some good information about how singletons work, how to use them, advantages/disadvantes, and so on. Thanks ------------------------------ "I''m a decorated astronaut, I don''t make those kind of mistakes." "Oh now wait a minute. Look I''ll show ya. I''ll enter the same calculations using what we like to call ''The Right Way''." -Rem Zirem Software
------------------------------"I'm a decorated astronaut, I don't make those kind of mistakes.""Oh now wait a minute. Look I'll show ya. I'll enter the same calculations using what we like to call 'The Right Way'."-RemZirem Software
MonkeyChuff
MonkeyChuff
OK, here is an example ....

  
// SingletonTest.cpp


#include "stdafx.h"
#include "singleton.h"
#include <iostream>

void main()
{
Singleton* mySingleton1 = Singleton::Instance();
Singleton* mySingleton2 = Singleton::Instance();

mySingleton1->set_TestVal(100);

std::cout << "TestVal From Singleton 2 = " << mySingleton2->get_TestVal() << "\n";
}

  
// Singleton.h


class Singleton
{
public:
static Singleton* Instance();
void set_TestVal(int testval);
const int get_TestVal() const;
protected:
Singleton();
private:
static Singleton* m_Instance;
int m_TestVal;
};

  
// Singleton.cpp


#include "stdafx.h"
#include "Singleton.h"

Singleton* Singleton::m_Instance = 0;

Singleton* Singleton::Instance()
{
if (m_Instance == 0)
{
m_Instance = new(Singleton);
}

return m_Instance;
}

Singleton::Singleton(): m_TestVal(0)
{

}

void Singleton::set_TestVal(int testval)
{
m_TestVal = testval;
}

const int Singleton::get_TestVal() const
{
return m_TestVal;
}


Hope it helps.
Remnex
Remnex
Thanks!



------------------------------
"I''m a decorated astronaut, I don''t make those kind of mistakes."
"Oh now wait a minute. Look I''ll show ya. I''ll enter the same calculations using what we like to call ''The Right Way''."

-Rem
Zirem Software
------------------------------"I'm a decorated astronaut, I don't make those kind of mistakes.""Oh now wait a minute. Look I'll show ya. I'll enter the same calculations using what we like to call 'The Right Way'."-RemZirem Software

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.