Kiekos
Member
|
GUI Singleton
Hey!
I've been trying to duplicate this tutorial with no successful outcome...
I don't have any problems with the part concerning checkboxes and other GUI elements like window and stuff. I can't get the whole 'class object creating' working.
Class declaration:
Code:
class Debugger
{
public:
Debugger::~Debugger();
static Debugger* Inst();
private:
Debugger::Debugger();
static Debugger pInst = NULL; //error here (won't let me set NULL)
}
Class definition:
Code:
Debugger::Debugger()
{
}
Debugger* Debugger::Inst()
{
if(pInst == NULL) //error here (cannot convert Debugger to Int type)
pInst = new Debugger(); //error here
return pInst; //error here (won't let return - different types:
// Debugger & Debugger*)
}
I'm wondering why I'm getting so many errors if it clearly works in that tutorial and I'm doing everything the same way...
Then there's another case - how do I create a singleton class object? Where do I call the creating function and how do I access the object afterwards? Should it be something like this in the main game file?
Code:
Debugger* dBug = Debugger::Inst(); //creating
(*dBug).someFunction(); //this type of accessing doesn't work either
Thanks in advance,
Kiekos
(This post was last modified: 01-31-2014 04:25 PM by Kiekos.)
|
|
01-31-2014 01:18 PM |
|
rstralberg
Member
|
RE: GUI Singleton
Here is an alternative
Header file
Code:
class Debugger
{
// Prevent this
Debugger();
Debugger( Debugger& );
void operator=(const Debugger&);
public:
// use instance
static Debugger& Instance();
// some arbitrary function
void func();
};
Source file
Code:
#include "debugger.h"
Debugger::Debugger()
{}
Debugger::Debugger( Debugger& )
{}
void Debugger::operator=(const Debugger&)
{}
Debugger& Debugger::Instance()
{
static Debugger instance;
return instance;
}
void Debugger::func()
{
}
Usage
Code:
Debugger::Instance().func();
(This post was last modified: 01-31-2014 02:53 PM by rstralberg.)
|
|
01-31-2014 02:37 PM |
|
Eric
Member
|
RE: GUI Singleton
Why don't you simply create a class/struct you wan't to use as a singleton and within it just block (private) destructor and copy operators. Create the instance only once (somewhere at the top of Main.cpp - "Singleton instance;") and then just use it as instance.foo(); with no static elements - that's how i do it. Just remember to not create it one more time somwhere else.
Code:
struct Singleton
{
void foo() {}
private:
~Singleton();
Singleton(Singleton&) {}
void operator=(C Singleton&) {}
}
Regards,
Eric 'Eri' Przychocki
ourgames.eu
(This post was last modified: 01-31-2014 03:44 PM by Eric.)
|
|
01-31-2014 03:40 PM |
|
rstralberg
Member
|
RE: GUI Singleton
(01-31-2014 03:40 PM)Eric Wrote: Why don't you simply create a class/struct you wan't to use as a singleton and within it just block (private) destructor and copy operators. Create the instance only once (somewhere at the top of Main.cpp - "Singleton instance;") and then just use it as instance.foo(); with no static elements - that's how i do it. Just remember to not create it one more time somwhere else.
Code:
struct Singleton
{
void foo() {}
private:
~Singleton();
Singleton(Singleton&) {}
void operator=(C Singleton&) {}
}
Besides that this code generates errors its not a Singleton.
The idea with a Singleton is that you should not have to remember
anything, just use it and rest assured that its the same instance each time.
(This post was last modified: 01-31-2014 03:49 PM by rstralberg.)
|
|
01-31-2014 03:47 PM |
|
Eric
Member
|
RE: GUI Singleton
I assume returning every time newly created Singleton instance in Instance() function is a better idea? Doesn't the Singleton.foo(); look more clear than Singleton::Instance().foo();?
P.S. Why it's not a singleton? If the programmer is careless enough to create another instance of Singleton I've nothing more to say.
Regards,
Eric 'Eri' Przychocki
ourgames.eu
|
|
01-31-2014 04:14 PM |
|
rstralberg
Member
|
RE: GUI Singleton
There are several way of doing it and I wont argue
Kiekos can choose which one he like as he now has two more alternatives to choose from and thats good.
|
|
01-31-2014 04:25 PM |
|
Kiekos
Member
|
RE: GUI Singleton
@Rubeus, if only it worked I would be extremely happy... Am I missing some pointer somewhere? And why won't it let me set the newly created static variable to NULL? In that tutorial it clearly works and I'd like to get it working as it's a nice and understood way (to me).
Thanks for the quick response guys! I think I'll try rstralberg's way of doing this in a sec.
|
|
01-31-2014 08:22 PM |
|
Rubeus
Member
|
RE: GUI Singleton
Kiekos, setting the static object to NULL doesn't work I believe because the linker treats static objects the same as global objects, and defining them is against some goofy standard or some such.
Try using rstralberg's singleton class. It's a very safe, effective, and pretty way to use a singleton(very similar to how I do mine).
Also, keep in mind that you don't "create" a singleton. By using (berg's)Debugger::Instance(), it allocates the memory automatically and keeps it there despite the function scope(due to static).
|
|
01-31-2014 10:06 PM |
|
Esenthel
Administrator
|
RE: GUI Singleton
Code:
Debugger& Debugger::Instance()
{
static Debugger instance;
return instance;
}
One way that this may break, is if you simultaneously call this function for the first time on 2 threads. To my knowledge function-scope statics are not always created thread safe.
A simple workaround to avoid critical sections, would be to create the instance always on the main thread before your custom code executes (in global constructor initialization phase).
|
|
01-31-2014 10:28 PM |
|
rstralberg
Member
|
RE: GUI Singleton
(01-31-2014 10:28 PM)Esenthel Wrote:
Code:
Debugger& Debugger::Instance()
{
static Debugger instance;
return instance;
}
One way that this may break, is if you simultaneously call this function for the first time on 2 threads. To my knowledge function-scope statics are not always created thread safe.
A simple workaround to avoid critical sections, would be to create the instance always on the main thread before your custom code executes (in global constructor initialization phase).
Good notation Esenthel. Thanks for that.
All that would be needed would that been a
Code:
Debugger::Instance();
somewhere in the main thread start up.
|
|
01-31-2014 10:48 PM |
|
Kiekos
Member
|
RE: GUI Singleton
Thanks guys! rstralberg's method works perfectly
Could you just explain to me the very first part?
I'm not familiar with this kind of declaration.
These two things after the constructor make me wonder:
Code:
Debugger();
Debugger( Debugger& );
void operator=(const Debugger&);
|
|
02-01-2014 07:11 PM |
|
TBJokers
Member
|
RE: GUI Singleton
(02-01-2014 07:11 PM)Kiekos Wrote: Thanks guys! rstralberg's method works perfectly
Could you just explain to me the very first part?
I'm not familiar with this kind of declaration.
These two things after the constructor make me wonder:
Code:
Debugger();
Debugger( Debugger& );
void operator=(const Debugger&);
Constructor - Debugger();
ConstructorWithArguments - Debugger(Debugger&); //Passing Debugger reference
Equals to function with constant debugger argument passed - void operator=(const Debugger&);
??
USAGE
Code:
Simple example of first constructor -> Debugger *debugger1 = new Debugger;
Second with arguments -> Debugger * debugger = new Debugger(*debugger1);
third is simple =
(This post was last modified: 02-01-2014 07:20 PM by TBJokers.)
|
|
02-01-2014 07:17 PM |
|
rstralberg
Member
|
RE: GUI Singleton
(02-01-2014 07:17 PM)TBJokers Wrote: Equals to function with constant debugger argument passed - void operator=(const Debugger&);
??
Copy operator
By making those constructors and the copy operator private
we are preventing the user from creating an instance other
that using the public Instance() method.
(This post was last modified: 02-01-2014 07:51 PM by rstralberg.)
|
|
02-01-2014 07:49 PM |
|