RE: [Dev-C++] Singleton with Reference?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Hooda, N. <nee...@cs...> - 2005-09-30 11:43:33
|
Omar=20
The problem is with statement
=20
Singleton1 sing =3D Singleton1::getHandle();
=20
And
=20
Singleton1 sing2 =3D Singleton1::getHandle();
=20
Here u are accepting the reference in an object so sing1 and sing2 are
two different objects now the question arises how is it possible to
create object when the constructor is private.Well the copy constructor
does the trick ,so change the above given lines to
=20
Singleton1 &sing =3D Singleton1::getHandle();
=20
Singleton1 &sing2 =3D Singleton1::getHandle();
=20
And for safety purpose define your copy constructor to be private.
=20
Regards
Neeraj
________________________________
From: dev...@li...
[mailto:dev...@li...] On Behalf Of Omar
Baqueiro
Sent: Friday, September 30, 2005 4:54 PM
To: dev...@li...
Subject: [Dev-C++] Singleton with Reference?
=20
Hello, I am trying to make a singleton class using a reference to an
object, this is the code:
---- BEGIN main.cpp ---
01 #include <cstdio>
02 #include <iostream>
03 =20
04 /*
05 * Singleton class
06 */
07 class Singleton1
08 {
09 int a,b,c;
10 =20
11 // class constructor
12 Singleton1(){}
13 public: =20
14 // class destructor
15 ~Singleton1(){} =20
16 static Singleton1 &getHandle()
17 {
18 static Singleton1 s; // create a static variable
to preserve its value while the prgram is running
19 return s;
20 }
21 void setVars(int na, int nb, int nc)
22 {
23 a =3D na;
24 b =3D nb;
25 c =3D nc;
26 }
27 void printVars()
28 {
29 printf ("a: %d, b: %d, c: %d\n",a,b,c);
30 }
31 =20
32 };
33 =20
34 using namespace std;
35 =20
36 int main(int argc, char *argv[])
37 {
38 Singleton1 sing =3D Singleton1::getHandle();
39 =20
40 puts("vars a,b,c have trash:");
41 sing.printVars();
42 =20
43 puts ("set vars to 1,2,3");
44 sing.setVars(1,2,3);
45 =20
46 puts ("vars a,b,c should have 1,2,3, they have:");
47 sing.printVars();
48 =20
49 puts ("Getting Singleton1 handler again...");
50 Singleton1 sing2 =3D Singleton1::getHandle();
51 =20
52 puts("[using new handle] vars a,b,c should have 1,2,3,
they have:");
53 sing2.printVars();
54 =20
55 system("PAUSE");
56 return EXIT_SUCCESS;
57 }
---- EOF ---
Now, the first lines until 49 on the main function behave as expected
but, on line 50 I try to get again the handle to the singleton (via
getHandle static function) but it returns another new variable so, the
values (1,2 and 3 in this case) of the previous Singleton object are
lost. Because of that line 53 will print 0,0,0 and not 1,2,3.
Am I doing something wrong here?=20
Now, I can make it work if I change the getHandle function to return a
pointer ( Singleton1 * getHandle() ) and I change also the static
variable [line 18] to=20
18 static Singleton1 s =3D new Singleton1();
Of course I also change the references to the variable. But if I do it
this way there is no way I can delete the dynamically allocated object.
And I would like to do it using references and avoid pointers.=20
Any clue?=20
Cheers,
Omar.
--=20
Omar Baqueiro Espinosa
Computer Systems Engineer
Home: www.csc.liv.ac.uk/~omar/
Calendar: calendar.yahoo.com/obaqueiro=20
PGP Key available at: www.csc.liv.ac.uk/~omar/pgp.html
_____
|