 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
joycemenger Guest
|
Posted: Thu Jul 06, 2006 4:23 am Post subject: 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! ] |
|
| Back to top |
|
 |
Guest
|
Posted: Thu Jul 06, 2006 3:31 pm Post subject: Re: singleton & auto_ptr (destructor is protected) |
|
|
Hi!
joycemenger schrieb:
| Quote: | 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.
class Test {
protected:
Test() {}
~Test() {}
public:
static Test& Instance();
};
Test& Test::Instance() {
static auto_ptr<Test> instance( new Test );
return *instance;
}
|
~auto_ptr<Test> calls ~Test via delete. You inhibit this by making
~Test protected. A solution would be: Make auto_ptr<Test> a friend of
Test.
Regards,
Matthias
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Gene Bushuyev Guest
|
Posted: Thu Jul 06, 2006 3:32 pm Post subject: Re: singleton & auto_ptr (destructor is protected) |
|
|
"joycemenger" <joycemenger (AT) gmail (DOT) com> wrote in message
news:1152094504.403892.251020 (AT) p79g2000cwp (DOT) googlegroups.com...
Unfortunately, that particular web site is full of erroneous information and bad
advise. I can't state it definitely regarding the mentioned article as I didn't
read it, rather just skimmed over some code snippets, but what I've noticed I
didn't like.
| Quote: |
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;
}
|
Compiler is correct rejecting this code: auto_ptr definitely needs destructor
~Test, which is inaccessible in this code. A solution can be to make auto_ptr a
friend of Test class. As a side note, a complication of using static auto_ptr
instead of static variable is only needed when the object is too big and must be
in the heap.
--
Gene Bushuyev (www.gbresearch.com)
----------------------------------------------------------------
To see what is in front of one's nose needs a constant struggle ~ George Orwell
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Guest
|
Posted: Fri Jul 07, 2006 3:09 am Post subject: Re: singleton & auto_ptr (destructor is protected) |
|
|
| Quote: |
class Test {
protected:
Test() {}
~Test() {}
public:
static Test& Instance();
};
Test& Test::Instance() {
static auto_ptr<Test> instance( new Test );
return *instance;
}
|
So, why do you need auto_ptr here?
You could just use static instance like this:
Test& Test::Instance() {
static Test instance;
return instance;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
johnchx2@yahoo.com Guest
|
Posted: Fri Jul 07, 2006 3:20 am Post subject: Re: singleton & auto_ptr (destructor is protected) |
|
|
joycemenger wrote:
| Quote: | class Test {
protected:
Test() {}
~Test() {}
public:
static Test& Instance();
};
Test& Test::Instance() {
static auto_ptr<Test> instance( new Test );
return *instance;
}
|
Unfortunately, the tutorial didn't do a good job of explaining why
you'd want a protected destructor in a class such as Test. Basically,
you make the dtor protected to prevent client code from doing something
ill-conceived like:
Test* p = & Test::Instance();
delete p;
Of course, a wise and careful programmer would never write such a
thing, at least not in this simple form. But the more "careless"
mistakes that can be transformed into compile-time errors, the better.
Of course, now *nobody* can call delete on a pointer to Test (since
delete calls the dtor and the dtor is protected). There are two ways
to fix that. One (discussed in other posts in this thread) is to make
auto_ptr<Test> a friend of Test. Another is to use inheritance:
struct MyTest: Test {};
to create a class with a public destructor, which implicitly calls the
protected destructor of its base class. Inside the Instance()
function, write:
static std::auto_ptr<MyTest> instance ( new MyTest );
You still return a Test& to the client (which is accessible because
Test is a public base class of MyTest).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|