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

Private Destructor - Use

P: n/a
Hi
What is the use declaring a dtor as private?
In one of the design tips on net, it is said that this prevents
possibiltiy of creating automatic variable of the class. Is this true?
Can this concept work in a Singleton?
How memory leak can be avoided since delete can not be called because
dtor is private?

Thanks in advance
Amol

Jun 7 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On 7 Juni, 07:51, Amol <Amol.Ga...@gmail.comwrote:
Hi
What is the use declaring a dtor as private?
In one of the design tips on net, it is said that this prevents
possibiltiy of creating automatic variable of the class. Is this true?
Yes
Can this concept work in a Singleton?
Yes
How memory leak can be avoided since delete can not be called
because dtor is private?
You have to create a special function that deletes instances of the
class, this can either be a static function or a member function. If I
were to use this I would make the constructor private too.

class Test
{
int i_;
~Test() {}
Test(int i) : i_(i) {}
public:
static Test* create(int i)
{
return new Test(i);
}
static void del(Test* t)
{
delete t;
}
};

int main()
{
Test* t1 = Test::create(4);
Test::del(t1);

Test* t2 = new Test(4); // Does not work
Test t3(4); // Does not work either
}

--
Erik Wikström

Jun 7 '07 #2

P: n/a
study...

Jun 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.