473,505 Members | 14,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is a C++ Singleton Class that Simple?

7 New Member
I saw a piece of code from a website. It seems to be a simple example for a singleton class. Basically, the author creates an object in the definition of a class, which has the same name as the class. Inside the definition of the class, everything is public. One cannot instantiate another object. Is it that simple to create a singleton class? Is the all-public members in the class good enough in terms of encapsulation?

In the following is the code run successfully:

#include "stdafx.h"
#include<stdio.h>
#include <iostream>
using namespace std;

class singleton
{
public:
void print()
{
printf("hello\n");
}
int a;
}singleton;

class client : public singleton
{};

int main()
{
// singleton s; // Can define an object before singleton
// singleton singleton;
// singleton b; #cannot define a new object after singleton
singleton.a=5;
printf("a=%d\n",singleton.a);
singleton.print();

client xyz;
xyz.print();
xyz.a = 10;
cout << "xyz.a = " << xyz.a << endl;

return 0;
}
Jul 24 '07 #1
3 3126
gpraghuram
1,275 Recognized Expert Top Contributor
I saw a piece of code from a website. It seems to be a simple example for a singleton class. Basically, the author creates an object in the definition of a class, which has the same name as the class. Inside the definition of the class, everything is public. One cannot instantiate another object. Is it that simple to create a singleton class? Is the all-public members in the class good enough in terms of encapsulation?

In the following is the code run successfully:

#include "stdafx.h"
#include<stdio.h>
#include <iostream>
using namespace std;

class singleton
{
public:
void print()
{
printf("hello\n");
}
int a;
}singleton;

class client : public singleton
{};

int main()
{
// singleton s; // Can define an object before singleton
// singleton singleton;
// singleton b; #cannot define a new object after singleton
singleton.a=5;
printf("a=%d\n",singleton.a);
singleton.print();

client xyz;
xyz.print();
xyz.a = 10;
cout << "xyz.a = " << xyz.a << endl;

return 0;
}

Hi,
This is not the right way to create a singleton class and in the example the class name is singleton and not the implementaion is.
Search in thie forum fo singleton class in c++.
Raghuram
Jul 24 '07 #2
plemoine
15 New Member
The singleton is a pattern where you want to make sure that no other instance can be created (you can look at "Effective C++" and "More Effective C++" from Scott Meyers).

Ex.1
Expand|Select|Wrap|Line Numbers
  1. class MySingleton {
  2.   public:
  3.     MySingleton() {
  4.         if (s_pInstance != NULL) {
  5.             //    run-time error
  6.         }
  7.     }
  8.  
  9.     ~MySingleton() {
  10.         if (s_pInstance != NULL) {
  11.             delete s_pInstance;
  12.             s_pInstance = NULL;
  13.         }
  14.     }
  15.  
  16.   private:
  17.     // prevents that an object of the class get copied
  18.     //    ex: MySingleton s2(s1);
  19.     //  ex: s2 = s1;
  20.     MySingleton(const MySingleton&);
  21.     MySingleton& operator=(const MySingleton&);
  22.  
  23.     static MySingleton*        s_pInstance;
  24. };
  25.  
  26. // in C++ file
  27. MySingleton*        MySingleton::s_pInstance = NULL;
  28.  
  29.  
In this example, it does a run-time check on the existence of a MySingleton instance.

Expand|Select|Wrap|Line Numbers
  1. class MySingleton2 {
  2.   public:
  3.     static MySingleton2& GetInstance();
  4.  
  5.   private:
  6.     MySingleton2() {
  7.         // ...
  8.     }
  9.  
  10.     ~MySingleton2() {
  11.         // ...
  12.     }
  13. };
  14.  
  15. // in C++ file
  16. MySingleton2& MySingleton2::GetInstance()
  17. {
  18.     static MySingleton2 theInstance;
  19.     return theInstance;
  20. }
  21.  
  22.  
In this example, the CTOR and DTOR are private, and the unique instance will be created on demand only, whenever GetInstance() gets called, so there is no error upon rt check.


I saw a piece of code from a website. It seems to be a simple example for a singleton class. Basically, the author creates an object in the definition of a class, which has the same name as the class. Inside the definition of the class, everything is public. One cannot instantiate another object. Is it that simple to create a singleton class? Is the all-public members in the class good enough in terms of encapsulation?

In the following is the code run successfully:

#include "stdafx.h"
#include<stdio.h>
#include <iostream>
using namespace std;

class singleton
{
public:
void print()
{
printf("hello\n");
}
int a;
}singleton;

class client : public singleton
{};

int main()
{
// singleton s; // Can define an object before singleton
// singleton singleton;
// singleton b; #cannot define a new object after singleton
singleton.a=5;
printf("a=%d\n",singleton.a);
singleton.print();

client xyz;
xyz.print();
xyz.a = 10;
cout << "xyz.a = " << xyz.a << endl;

return 0;
}
Jul 24 '07 #3
sicarie
4,677 Recognized Expert Moderator Specialist
There is another example in the C/C++ Articles section.

http://www.thescripts.com/forum/thread656124.html
Jul 24 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

16
6683
by: cppaddict | last post by:
Hi, In this tutorial on singleton class in C++ (http://gethelp.devx.com/techtips/cpp_pro/10min/10min0200.asp) the author gives two implementations of a simple singleton class, claiming that...
7
3251
by: Ethan | last post by:
Hi, I have a class defined as a "Singleton" (Design Pattern). The codes are attached below. My questions are: 1. Does it has mem leak? If no, when did the destructor called? If yes, how can I...
5
5293
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
12
8920
by: Preets | last post by:
Can anyone explain to me the exact use of private constructors in c++ ?
6
3163
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the...
5
21146
by: Damien | last post by:
Hi all, I'm using a pretty standard C++ Singleton class, as below: template <typename T> class Singleton { public: static T* Instance() {
3
18224
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
2
1571
by: Bob Johnson | last post by:
Just wondering the extent to which some of you are implementing classes as Singletons. I'm working on a brand new project and, early on, identified some obvious candidates. By "obvoius candidates"...
4
2812
by: John Doe | last post by:
Hi, I have a singleton class defined like this : class UIManager : public CSingleton<UIManager>, public CObject { protected: DECLARE_DYNAMIC(UIManager) friend class CSingleton<UIManager>;
0
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5613
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5028
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
407
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.