singleton & auto_ptr (destructor is protected)
Hi, I was reading this online article
http://www.codeproject.com/cpp/singleton.asp
and tried to write some code to do something similar. Although when i
compile it i get the following message which seems logical to me. Any
idea how to make that work ? I suspect the author of the article knows
what he is talking about so it must something that i am missing.
Thank you, joyce
* code *
class Test {
protected:
Test() {}
~Test() {}
public:
static Test& Instance();
};
Test& Test::Instance() {
static auto_ptr<Test> instance( new Test );
return *instance;
}
int main() {
// call Test::Instance() here
}
* error message *
test.cpp: In function `int main()':
test.cpp:120: error: 'Test::Test()' is protected
test.cpp:127: error: within this context
/usr/include/gcc/darwin/4.0/c++/memory: In destructor
`std::auto_ptr<_Tp>::~auto_ptr() [with _Tp = Test]':
test.cpp:127: instantiated from here
test.cpp:121: error: 'Test::~Test()' is protected
/usr/include/gcc/darwin/4.0/c++/memory:260: error: within this context
JMenger-Computer:~/singleton_test jmenger$ c++ -o test test.cpp
/usr/include/gcc/darwin/4.0/c++/memory: In destructor
`std::auto_ptr<_Tp>::~auto_ptr() [with _Tp = Test]':
test.cpp:127: instantiated from here
test.cpp:121: error: 'Test::~Test()' is protected
/usr/include/gcc/darwin/4.0/c++/memory:260: error: within this context
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]