473,785 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using singleton class instance in another class

Hi, I got a singleton class. Then in another class (CA), i would declare a
variable with this singleton class and get a reference to it. I would have
another class (IA1) that would inherit from CA and I would want to allow IA1
to access the singleton class. My question is is it better to declare the
singleton class as private or protected in CA? Or shall I let IA1 to get a
reference to the singleton by itself?

Thanks.
Eugene
Nov 17 '05 #1
2 1804
Hi Eugene,
make the variable protected so that the base and derived class use the
same member variable to reference the Singleton. One benefit of inheritance
is to allow code reuse from the base class.

Mark R Dawson
http://www.markdawson.org
"Eugene" wrote:
Hi, I got a singleton class. Then in another class (CA), i would declare a
variable with this singleton class and get a reference to it. I would have
another class (IA1) that would inherit from CA and I would want to allow IA1
to access the singleton class. My question is is it better to declare the
singleton class as private or protected in CA? Or shall I let IA1 to get a
reference to the singleton by itself?

Thanks.
Eugene

Nov 17 '05 #2
Mark R. Dawson <Ma*********@di scussions.micro soft.com> wrote:
make the variable protected so that the base and derived class use the
same member variable to reference the Singleton. One benefit of inheritance
is to allow code reuse from the base class.


Alternatively (and preferrably, IMO) provide a protected *property*
rather than a protected variable. If you use the property everywhere in
both classes, that gives you more flexibility if you later change from
the singleton to a factory, for instance.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
5877
by: Sean Dettrick | last post by:
Hi, apologies for yet another Singleton posting. From reading around previous postings etc, I've cobbled together two Singleton template definitions - one that returns a reference, and one that returns a pointer. I understand the pointer one and it works, but the reference one doesn't work as I expect. Can anybody explain this to me? (Code below). I'm not really looking for thread safety or for something as general
1
2453
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h template <class T> class Singleton : public T { public: static T * Instance();
2
1689
by: davidw | last post by:
As I asked in last post, I want to put some logic in a object and all my webcontrol instance will access that object, the object is responsed to retrieve data from database if the data has not been retrieved yet. Vadym Stetsyak suggested me to use singleton, it seems a good solution for me, but after I read more about singleton, I find another issue - if I use singleton patter, I can not store context data in the class anymore. How I used...
9
14148
by: Marcel Hug | last post by:
Hallo NG ! I Have a little question about inheritance of a singleton class. In my application i have a Database-Connection Lib, in which I would ¨like to connect different databases of the same project (thats why it is in a lib). For the database A I created a singleton class AProxy and for the database B the same as BProxy. Initializing and connetcing to the Database is the same in both classes (except of the database-path, which is a...
2
6444
by: Tim | last post by:
How can I expose a Singleton C# class to COM? I have implemented the singleton pattern for the C# class. The 'constructor' of the class is protected. Now, I want to expose this class to a VB6 exe. But COM expects the default constructor of the class to be available for instantiating it through Interop. How can I create and access the functionality of the C# Singleton class from VB6 Exe?
7
4234
by: John A Grandy | last post by:
For a singleton class utilizes by ASP.NET 2.0 page processing: When initial instantiation is performed during the initial call to the retrieve instance method (let's call the method "getInstance()"), an instantiated object of the class is assigned to the class' internal static reference to that an object of itself (let's call the reference "uniqueInstance") Does that uniqueInstance have global scope in the context of page processing ?
7
1430
by: ThunderMusic | last post by:
Hi, I have a problem regarding singletons in C#. What I would like to do is the following ClassA is a singleton ClassB inherits from ClassA and is also a Singleton there cannot and instance of ClassA and an instance of ClassB, there must be only one of one or the other... Is there a way to do it? or it's simply not possible?
5
21163
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() {
10
1527
by: =?Utf-8?B?ZGF2ZWJ5dGhlc2Vh?= | last post by:
Hi, I have created a Singleton class to provide some database functionality in my mobile application. I have a public class called Utility which performs various operations on data. Is it ok to use the utility class in my Singleton class? Here is some code to hopefully make it clearer what i want to do - namespace MyApp { class SingletonConnection
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8978
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5383
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
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 we have to send another system
3
2881
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.