Question 1:
What is a singleton? What are the advantages and disadvantages of using one?
Optional:
Write either a base class or template class to ease the implementation of a singleton.
Question 2:
Why do you want to work here?
Go for it!
Posted 21 February 2011 - 10:06 AM

POPULAR
Posted 21 February 2011 - 10:47 AM
stayscrisp, on 21 February 2011 - 10:06 AM, said:
CSingleton mySingletone;
int main(int argc, char** argv)
{
mySingletone = new CSingleton();
bool forever = true;
do{
forever = mySingletone.Update();
}while(forever);
}
class CSingleton
{
CClass1 myClass1;
CClass2 myClass2;
CPlayer myPlayer;
int x,z,y;
void CSingleton()
{
// This becomes my initialization spot
myPlayer = CPlayer();
x = 0;
y = 0;
z = 0;
}
void ~CSingleton()
{
// Clear everything
}
bool Update();
}
This post has been edited by (Cryptic): 21 February 2011 - 10:48 AM
Posted 21 February 2011 - 01:17 PM
Posted 21 February 2011 - 04:56 PM
This post has been edited by stayscrisp: 22 February 2011 - 05:11 AM
Posted 22 February 2011 - 05:37 AM
stayscrisp, on 21 February 2011 - 10:06 AM, said:
template<class T>
class Singleton
{
public:
static T* Instance()
{
// Meyers singleton
static T obj;
return &obj;
}
};
This post has been edited by diego_pmc: 22 February 2011 - 06:06 AM
Reason for edit:: Added spoiler tags
Posted 22 February 2011 - 05:54 AM
#include "Singleton.h"
class Foo
{
public:
// some methods
private:
Foo(); // private constructor
friend class Singleton<Foo>; // you can see that this class is a singleton
};
typedef Singleton<Foo> TheFoo; // a nice typedef
Posted 22 February 2011 - 06:02 AM
This post has been edited by diego_pmc: 22 February 2011 - 06:06 AM
Posted 22 February 2011 - 06:14 AM
template<class T>
class Singleton
{
public:
static T* Instance()
{
static T obj;
return &obj;
}
};
class Foo
{
friend Singleton;
protected:
Foo();
Foo(const Foo& other);
~Foo();
Foo& operator=(const Foo& other);
};
class Foo
{
public:
static Foo* Instance();
protected:
Foo();
Foo(const Foo& other);
~Foo();
Foo& operator=(const Foo& other);
};
This post has been edited by diego_pmc: 22 February 2011 - 06:49 AM
Posted 22 February 2011 - 06:14 AM
#include "Factory.h" #include "Singleton.h" typedef Factory<GameObject> GameObjectFactory; typedef Singleton<GameObjectFactory> TheGameObjectFactory;
Posted 22 February 2011 - 08:53 AM
This post has been edited by diego_pmc: 22 February 2011 - 08:54 AM
Posted 22 February 2011 - 11:27 AM
template <class T>
class Singleton {
private:
static T *instance;
public:
static T* getInstance() {
if (instance == NULL) {
instance = new T;
}
return instance;
}
};
template <class T> T* Singleton<T>::instance=NULL;
|
|
Query failed: connection to localhost:3312 failed (errno=111, msg=Connection refused).
|
