0

Possible Duplicate:
C++ Singleton design pattern.

How can I create only one instance of a class and share that instance with all my header and source files without using a singleton? Can you provide a simple example?

Community
  • 1
  • 1
jose gabriel
  • 754
  • 2
  • 8
  • 21
  • 2
    Er, isn't that pretty much the definition of a Singleton? – James Jan 25 '11 at 19:22
  • What do you mean by "share that instance"? And, homework? – pascal Jan 25 '11 at 19:22
  • i know that is the definition of a singleton but i need to know if there is another way to do the same. when i say to share the instance i mean to use a that only instance created in all my source and headers. i also would like and example :). thank you – jose gabriel Jan 25 '11 at 19:25
  • Maybe "using a singleton" means implementing the constraint in the class itself. – pascal Jan 25 '11 at 19:26
  • 2
    I don't think it's a duplicate; he seems to know about Singleton, and he doesn't want it. Looks like it doesn't solve his problem;. that is why he is asking a solution *"without using a singleton"*. – Nawaz Jan 25 '11 at 19:47

3 Answers3

3

You can do this:

class Sample
{
   /*** your code **/
   public:
    Sample();
    void DoWork();
    int  GetValue();
  /*** other functions ***/
};

Sample & OneInstance()
{
    static Sample instance;
    return instance;
}

//Use OneInstance everywhere like this
OneInstance().DoWork();

Note Sample is not a Singleton, but you can use OneInstance() function as if it's one and the same instance of Sample, which you use everywhere!

You can use it to initialize some global variables also like this:

int g_SomeValue= OneInstance().GetValue();

which cannot be done with global static instance of Sample. That is because of this:

static initialization order fiasco

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • the problem is that that example allows anyone to create instances of sample. – jose gabriel Jan 25 '11 at 19:35
  • 1
    @jose: exactly. that is what is meant by not being Singleton. That is not the problem. The OP wants one instance which he can use throughout the program. He may want to create multiple instances in some other context, or situation. He might be facing a situation where he want to make such a class, or such a global instance. I'm just trying to help him, given that I don't know exactly what he is facing. Maybe, he doesn't need it at all, or maybe he needs it. Who knows. But what I can do, answer his question. Maybe, it'll help him! – Nawaz Jan 25 '11 at 19:37
  • and what if i make the class in a header declare a global instance of that class as const and use it in my other sources and headers?? can it be done?? – jose gabriel Jan 25 '11 at 19:47
  • Sample & OneInstance() { static Sample instance; return instance; } this will always be the same instance??? – jose gabriel Jan 25 '11 at 19:52
  • @jose: `const` instance cannot be changed, maybe he doesn't want it.Also my solution is not going to solve everything, no solution can solve everything. I don't know if he wants `const` instance. So far he didn't say that. – Nawaz Jan 25 '11 at 19:53
  • @jose: oops... I didn't notice that you're the OP :D. Now tell me what exactly do you want? – Nawaz Jan 25 '11 at 19:54
  • @jose: Yes, that will always be the same instance. `static` variable is created once, even inside the function! – Nawaz Jan 25 '11 at 19:55
  • another question is: i read in a book. that when i have to make a singleton my static class* intance(); function must be declared in a cpp file because otherwise"an instance will be created for every file in wich the header is include". i really dont understand that. why that happens?? – jose gabriel Jan 25 '11 at 20:24
  • @jose: no. you define the function in `.cpp` file, otherwise if you define it in `.h` header, then you'll have multiple definitions of that function, as you might `#include` the same `.h` header in multiple files. So just to avoid this multiple definition, you define functions in `.cpp` file! – Nawaz Jan 25 '11 at 20:30
  • mmmmm so if i develop that function into de .h file i will create one instance in every file i #include my header its like im going to have n statics instances of the same class. where n is the number of files i include my header.?? am i wrong? – jose gabriel Jan 25 '11 at 20:36
  • @jose: try doing that. you'll get compilation error along these lines : *multiple definition of function; or redefinition of function*. something like that. by the way, don't ask only questions, try experiment with it; that is one of the best way of learning. :-) – Nawaz Jan 25 '11 at 20:40
  • thank you very much @Nawaz you really helped me. im kind of noob i do programming for hobbie. im in my last year of medicine and programming help me with the stress. if you need help with health stuff contact me. sorry for my english; is very bad :D. thank you again. – jose gabriel Jan 25 '11 at 20:48
2

Don't do.

Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
1

I'd advice against sharing anything whenever you can avoid it, because sharing makes concurrency hard. If you don't care about concurrency then you should pass your object around as an extra parameter to functions. Globals are usually a bad idea and Singletons are usually mere globals with a fancy dress.

wilhelmtell
  • 57,473
  • 20
  • 96
  • 131