473,753 Members | 6,232 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

singleton for a parent with protected destructor

hello, in a singleton design, i trying to make my parent class having
protected constructor and destructor. however, the compiler give me
error when my child, trying to delete itself through parent pointer.

isn't child should have access to parent protected destructor?

thank you.
class CMachineFactory
{
protected:
CMachineFactory () {}
virtual ~CMachineFactor y() {}
_______________ _______________ _______________ _____________
CMachineFactory * CSemiAutoMachin eFactory::m_pIn stance = NULL;

class CSemiAutoMachin eFactory : public CMachineFactory
{
protected:
CSemiAutoMachin eFactory();
virtual ~CSemiAutoMachi neFactory();

void CSemiAutoMachin eFactory::Delet eInstance()
{
if(m_pInstance != NULL) {
delete m_pInstance; // Why not alowed. Isn't child is allowed
// to access parent's memeber?

m_pInstance = NULL;
}
}
E:\xxx\semiauto machinefactory. cpp(214) : error C2248:
'CMachineFactor y::~CMachineFac tory' : cannot access protected member
declared in class 'CMachineFactor y'
E:\Work\rd00072 \ScanProxyEx\ma chinefactory.h( 43) : see
declaration of 'CMachineFactor y::~CMachineFac tory'

Sep 12 '05 #1
2 3913

yc*****@gmail.c om wrote:
hello, in a singleton design, i trying to make my parent class having
protected constructor and destructor. however, the compiler give me
error when my child, trying to delete itself through parent pointer.

isn't child should have access to parent protected destructor?

thank you.
class CMachineFactory
{
protected:
CMachineFactory () {}
virtual ~CMachineFactor y() {}
_______________ _______________ _______________ _____________
CMachineFactory * CSemiAutoMachin eFactory::m_pIn stance = NULL;

class CSemiAutoMachin eFactory : public CMachineFactory
{
protected:
CSemiAutoMachin eFactory();
virtual ~CSemiAutoMachi neFactory();

void CSemiAutoMachin eFactory::Delet eInstance()
{
if(m_pInstance != NULL) {
delete m_pInstance; // Why not alowed. Isn't child is allowed
// to access parent's memeber?

m_pInstance = NULL;
}
}
E:\xxx\semiauto machinefactory. cpp(214) : error C2248:
'CMachineFactor y::~CMachineFac tory' : cannot access protected member
declared in class 'CMachineFactor y'
E:\Work\rd00072 \ScanProxyEx\ma chinefactory.h( 43) : see
declaration of 'CMachineFactor y::~CMachineFac tory'


This is a non-compliant issue with VC++ compiler. The code should
compile, but it will not in VC++ 6.0 or 7.1.

As a workaround, you can create a friend class and let it delete your
singleton.
Here's a workaround example:
template<typena me T>
class Singleton
{
protected:
Singleton(){}
~Singleton(){}
Singleton(const Singleton&);
Singleton& operator=(const Singleton&);
public:
class FriendClass
{
public:
FriendClass():m _MyClass(new T()){}
~FriendClass(){ delete m_MyClass;}
T* m_MyClass;
};
static T& Instance() {
static FriendClass Instance;
return *Instance.m_MyC lass;
//Will not compile with the following code in VC++ 6.0 or 7.1
// static T Instance;
// return Instance;
}
};

//The above Singleton template can be used two ways.
//Either by deriving from it, or by declaring the friend class with
fully
//qualified template, and calling the instance member function with
fully
//qualified template function.

class Widget {
private:
Widget(){}
~Widget(){}
Widget& operator=(const Widget&);
Widget(const Widget&);
public:
friend class Singleton<Widge t>::FriendClass ;
int m_i;
};
class foo : public Singleton<foo>{
private:
foo(){}
~foo(){}
foo& operator=(const foo&);
foo(const foo&);
public:
friend class FriendClass;
int m_i;
};

int main(int, char*)
{
Widget& MyWidget = Singleton<Widge t>::Instance() ;
foo& Myfoo = foo::Instance() ;

Sep 12 '05 #2
<yc*****@gmail. com> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.com
hello, in a singleton design, i trying to make my parent class having
protected constructor and destructor. however, the compiler give me
error when my child, trying to delete itself through parent pointer.

isn't child should have access to parent protected destructor?

thank you.
class CMachineFactory
{
protected:
CMachineFactory () {}
virtual ~CMachineFactor y() {}
_______________ _______________ _______________ _____________
CMachineFactory * CSemiAutoMachin eFactory::m_pIn stance = NULL;

class CSemiAutoMachin eFactory : public CMachineFactory
{
protected:
CSemiAutoMachin eFactory();
virtual ~CSemiAutoMachi neFactory();
These half-declared classes make it unnecessarily difficult for anyone to
respond to your post. You should post code that is sufficiently complete so
that it compiles or, in your judgement, "ought" to compile.
void CSemiAutoMachin eFactory::Delet eInstance()
{
if(m_pInstance != NULL) {
delete m_pInstance; // Why not alowed. Isn't child is allowed
// to access parent's memeber?

m_pInstance = NULL;
}
}


A derived class only has access to protected members in base subobjects of
that class. It does not have general access to protected members in the base
class, e.g., if Foo derives from CMachineFactory , then
CSemiAutoMachin eFactory has no protected access rights to the
CMachineFactory part of Foo. Likewise CSemiAutoMachin eFactory has no
protected access rights to self-standing instances of CMachineFactory .

CMachineFactory * CSemiAutoMachin eFactory::m_pIn stance;

is a pointer to a CMachineFactory object which need not be a subobject of a
CSemiAutoMachin eFactory object. Because of this, CSemiAutoMachin eFactory
cannot access protected members of CMachineFactory via this pointer.
--
John Carson

Sep 12 '05 #3

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

Similar topics

2
5875
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
7
12479
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a) explicitly make the arbitrary class's constructor and destructor private b) declare the Singleton a friend of the arbitrary class
3
2498
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic what sort of problems/issues should be considered? Also, I see that a singleton needs to be set up with certain data such as file name, database URL etc. What issues are involved in this, and how would you do this? If someone knows about the...
7
3288
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 avoid it? Purify does not show it has mem leak. // test if singleto class has mem leakage #include <iostream>
5
5310
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++ Design" or similar books, but I feel there are full of clever guys here who could help me out.
5
1743
by: Eric | last post by:
I am implementing a variation on the Singleton design pattern, that allows up to 8 objects of a class to be instantiated, and returns a null pointer for anything more than 8. I am running into a compile problem with GNU g++. Here is the code: /******************** FILE sample.h ********************/
3
18251
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 is, regardless of where the object is hidden, everyone needs access to it. The global point of access is the object's Instance() method. Individual users need to be prevented from creating their own instances of the Singleton.
1
4165
by: James Kanze | last post by:
On Apr 11, 12:09 am, Ron Eggler <t...@example.comwrote: That's usually a feature, not a bug, where singletons are concerned. In general, I avoid ever deleting a singleton. From the destructor of what?
3
1802
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design constraints/environment are as follows: 1) Everything is single-threaded during static initialization (as in prior to the open brace of main) 2) The environment may be multi-threaded during nominal program execution (within {} of main) 3) I...
0
8896
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9653
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9333
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8328
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
6869
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
6151
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3395
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
2
2872
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2284
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.