By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,650 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,650 IT Pros & Developers. It's quick & easy.

singleton for a parent with protected destructor

P: n/a
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 ~CMachineFactory() {}
__________________________________________________ ________
CMachineFactory* CSemiAutoMachineFactory::m_pInstance = NULL;

class CSemiAutoMachineFactory : public CMachineFactory
{
protected:
CSemiAutoMachineFactory();
virtual ~CSemiAutoMachineFactory();

void CSemiAutoMachineFactory::DeleteInstance()
{
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\semiautomachinefactory.cpp(214) : error C2248:
'CMachineFactory::~CMachineFactory' : cannot access protected member
declared in class 'CMachineFactory'
E:\Work\rd00072\ScanProxyEx\machinefactory.h(43) : see
declaration of 'CMachineFactory::~CMachineFactory'

Sep 12 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

yc*****@gmail.com 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 ~CMachineFactory() {}
__________________________________________________ ________
CMachineFactory* CSemiAutoMachineFactory::m_pInstance = NULL;

class CSemiAutoMachineFactory : public CMachineFactory
{
protected:
CSemiAutoMachineFactory();
virtual ~CSemiAutoMachineFactory();

void CSemiAutoMachineFactory::DeleteInstance()
{
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\semiautomachinefactory.cpp(214) : error C2248:
'CMachineFactory::~CMachineFactory' : cannot access protected member
declared in class 'CMachineFactory'
E:\Work\rd00072\ScanProxyEx\machinefactory.h(43) : see
declaration of 'CMachineFactory::~CMachineFactory'


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<typename 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_MyClass;
//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<Widget>::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<Widget>::Instance();
foo& Myfoo = foo::Instance();

Sep 12 '05 #2

P: n/a
<yc*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.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 ~CMachineFactory() {}
__________________________________________________ ________
CMachineFactory* CSemiAutoMachineFactory::m_pInstance = NULL;

class CSemiAutoMachineFactory : public CMachineFactory
{
protected:
CSemiAutoMachineFactory();
virtual ~CSemiAutoMachineFactory();
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 CSemiAutoMachineFactory::DeleteInstance()
{
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
CSemiAutoMachineFactory has no protected access rights to the
CMachineFactory part of Foo. Likewise CSemiAutoMachineFactory has no
protected access rights to self-standing instances of CMachineFactory.

CMachineFactory* CSemiAutoMachineFactory::m_pInstance;

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

Sep 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.