About Store Forum Documentation Contact



Post Reply 
GUI Singleton
Author Message
Kiekos Offline
Member

Post: #1
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
Find all posts by this user Quote this message in a reply
rstralberg Offline
Member

Post: #2
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
Visit this user's website Find all posts by this user Quote this message in a reply
Eric Offline
Member

Post: #3
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. smile

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
Find all posts by this user Quote this message in a reply
rstralberg Offline
Member

Post: #4
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. smile

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
Visit this user's website Find all posts by this user Quote this message in a reply
Eric Offline
Member

Post: #5
RE: GUI Singleton
I assume returning every time newly created Singleton instance in Instance() function is a better idea? pfft 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. smile

Regards,
Eric 'Eri' Przychocki
ourgames.eu
01-31-2014 04:14 PM
Find all posts by this user Quote this message in a reply
rstralberg Offline
Member

Post: #6
RE: GUI Singleton
There are several way of doing it and I wont argue smile
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
Visit this user's website Find all posts by this user Quote this message in a reply
Pixel Perfect Offline
Member

Post: #7
RE: GUI Singleton
(01-31-2014 04:14 PM)Eric Wrote:  ...
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. smile
Because by definition a singleton is a class which only allows a single instance of itself ... hence the use of a static instance.
01-31-2014 05:19 PM
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #8
RE: GUI Singleton
(01-31-2014 04:14 PM)Eric Wrote:  I assume returning every time newly created Singleton instance in Instance() function is a better idea? pfft

In his example, the 'newly created' singleton is a static variable, so it's returning the same one every time. It's a very elegant way of using singletons in my opinion.
01-31-2014 07:13 PM
Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #9
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
Find all posts by this user Quote this message in a reply
Rubeus Offline
Member

Post: #10
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
Find all posts by this user Quote this message in a reply
Esenthel Offline
Administrator

Post: #11
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
Find all posts by this user Quote this message in a reply
rstralberg Offline
Member

Post: #12
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
Visit this user's website Find all posts by this user Quote this message in a reply
Kiekos Offline
Member

Post: #13
RE: GUI Singleton
Thanks guys! rstralberg's method works perfectly smile

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
Find all posts by this user Quote this message in a reply
TBJokers Offline
Member

Post: #14
RE: GUI Singleton
(02-01-2014 07:11 PM)Kiekos Wrote:  Thanks guys! rstralberg's method works perfectly smile

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
Visit this user's website Find all posts by this user Quote this message in a reply
rstralberg Offline
Member

Post: #15
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
Visit this user's website Find all posts by this user Quote this message in a reply
Post Reply