Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 3

Qt-interest Archive, June 2007
ot(?): static c++ class visible from everywhere


Message 1 in thread

Dear Experts,

I have set of global parameters (like last used values in dialog boxes etc)
which i'd like to store in the class. I want the class to be static and
visible from everywhere.
Is there any way how to do it without writing extern <blablabla> at the
beginning of my code (or include containing this extern - this is less
harming)? I dont think that singleton is the same thing. Correct me if I'm
wrong, but
if you have singleton, each time you call global function it creates new
static instance of the class, which is destroyed when one leaves the global
funtion. This is not what I want,
I'd like to have one static class visible during the life of the
application.....


thanks
d.

Message 2 in thread

On 13.06.07 23:55:35, dejfson wrote:
> Dear Experts,
> 
> I have set of global parameters (like last used values in dialog boxes etc)
> which i'd like to store in the class. I want the class to be static and
> visible from everywhere.
> Is there any way how to do it without writing extern <blablabla> at the
> beginning of my code (or include containing this extern - this is less
> harming)?a I dont think that singleton is the same thing. Correct me if I'm
> wrong, but
> if you have singleton, each time you call global function it creates new
> static instance of the class, which is destroyed when one leaves the global
> funtion. This is not what I want,

Wrong, singleton means there exists only 1 instance of that class in
your application. That singleton is normally created during the first
access and not deleted until your application exits.

Andreas

-- 
 [ signature omitted ] 

Message 3 in thread

hmm,

if i have public function like this one:

Q_configuration& config()
{
    static Q_configuration conf;
    return conf;
}

where Q_configuration is my singleton class ....

everytime I call config()-><somefunction> it calls again and again
constructor of conf.

why is that?

Message 4 in thread

dejfson wrote:
> hmm,
>
> if i have public function like this one:
>
> Q_configuration& config()
> {
>     static Q_configuration conf;
>     return conf;
> }
>
> where Q_configuration is my singleton class ....
>
> everytime I call config()-><somefunction> it calls again and again
> constructor of conf.
>
> why is that?
Because static in a header means something totally different to static
in a .cpp. Move your function to a .cpp, and only declare it in a header
file, and it'll work correctly.

(static in a header file means "static to this .cpp file", so every
single .cpp file you include it into will contain it's own copy).

-- 
 [ signature omitted ] 

Message 5 in thread

And there are several ways to implement a singleton in C++.  One is a 
class that is made up purely of static methods and members which is 
never instantiated.  And you don't need extern blabla for that.  Another 
is to implement the class such that it creates a single instance for 
itself when callers call a ::getInstance method.  For example:

class Foo
public:
    ...
    static Foo *getInstance()
    {
       if (!myInstance)
          myInstance = new Foo();
       return myInstance;
    }
    ...
private:
    Foo() {...}  // ensures that this is a singleton
    Foo(Foo const &);  // no copies allowed
    Foo & operator=(Foo const &);  // again, no copies
    ...
    static Foo *myInstance;
    ...
};

I didn't compile this code, it's just for illustrative purposes. 

Depending on your needs you can go either way.

HTH,
Susan

Andreas Pakulat wrote:
> On 13.06.07 23:55:35, dejfson wrote:
>   
>> Dear Experts,
>>
>> I have set of global parameters (like last used values in dialog boxes etc)
>> which i'd like to store in the class. I want the class to be static and
>> visible from everywhere.
>> Is there any way how to do it without writing extern <blablabla> at the
>> beginning of my code (or include containing this extern - this is less
>> harming)?a I dont think that singleton is the same thing. Correct me if I'm
>> wrong, but
>> if you have singleton, each time you call global function it creates new
>> static instance of the class, which is destroyed when one leaves the global
>> funtion. This is not what I want,
>>     
>
> Wrong, singleton means there exists only 1 instance of that class in
> your application. That singleton is normally created during the first
> access and not deleted until your application exits.
>
> Andreas
>
>   



---

This communication contains confidential information. If you are not the intended recipient please return this email to the sender and delete it from your records.

Diese Nachricht enthält vertrauliche Informationen. Sollten Sie nicht der beabsichtigte Empfänger dieser E-mail sein, senden Sie bitte diese an den Absender zurück und löschen Sie die E-mail aus Ihrem System.


Message 6 in thread

Susan Macchia schrieb:
> ...
> private:
>     Foo() {...}  // ensures that this is a singleton
>     Foo(Foo const &);  // no copies allowed
>     Foo & operator=(Foo const &);  // again, no copies
>     ...
>     static Foo *myInstance;

That's pretty much it, just to add: you should at least make the c'tor 
protected, else some compilers (gcc of some version) will complain 
(warning) about "Foo can not be created" or something (which in fact is 
exactly what we want here, but the compiler does not understand this ;)

Cheers, Oliver
-- 
 [ signature omitted ] 

Message 7 in thread

Thanks to all, i think now I understand what I want :)