469,579 Members | 1,089 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,579 developers. It's quick & easy.

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 ~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
2 3731

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
<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.

Similar topics

2 posts views Thread by Sean Dettrick | last post: by
7 posts views Thread by Tim Clacy | last post: by
3 posts views Thread by Alicia Roberts | last post: by
7 posts views Thread by Ethan | last post: by
5 posts views Thread by Pelle Beckman | last post: by
1 post views Thread by James Kanze | last post: by
3 posts views Thread by stevewilliams2004 | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.