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

what's the trick of making virtual destructor protedted?

P: n/a
I wonder if there is any reasonable reason to make virtual destructor
protected within a derived class purposely?

Jul 3 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
In article <11**********************@a14g2000cwb.googlegroups .com>,
da*************@gmail.com says...
I wonder if there is any reasonable reason to make virtual destructor
protected within a derived class purposely?
A virtual protected dtor will normally be accompanied by a static
member function to create objects. The dtor is protected to prevent
outside code from creating instances of the class without using the
static member function to create them. Making it virtual and
protected means this is intended to be used as a base class.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 3 '06 #2

P: n/a
Jerry Coffin wrote:
In article <11**********************@a14g2000cwb.googlegroups .com>,
da*************@gmail.com says...
>I wonder if there is any reasonable reason to make virtual destructor
protected within a derived class purposely?

A virtual protected dtor will normally be accompanied by a static
member function to create objects. The dtor is protected to prevent
outside code from creating instances of the class without using the
static member function to create them. Making it virtual and
protected means this is intended to be used as a base class.
Are you confusing _dtor_ with _ctor_?

Shimin
Jul 3 '06 #3

P: n/a

Jerry Coffin wrote:
In article <11**********************@a14g2000cwb.googlegroups .com>,
da*************@gmail.com says...
I wonder if there is any reasonable reason to make virtual destructor
protected within a derived class purposely?

A virtual protected dtor will normally be accompanied by a static
member function to create objects. The dtor is protected to prevent
outside code from creating instances of the class without using the
static member function to create them. Making it virtual and
protected means this is intended to be used as a base class.

--
I think the destructor has nothing to do with the way that the object
is created, so I guess you are talking about constructor? or did I miss
anything? will the protected destructor effect the way that my object
is destroyed?

thanks again.

daniel
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 3 '06 #4

P: n/a
* Shimin:
Jerry Coffin wrote:
>In article <11**********************@a14g2000cwb.googlegroups .com>,
da*************@gmail.com says...
>>I wonder if there is any reasonable reason to make virtual destructor
protected within a derived class purposely?

A virtual protected dtor will normally be accompanied by a static
member function to create objects. The dtor is protected to prevent
outside code from creating instances of the class without using the
static member function to create them. Making it virtual and protected
means this is intended to be used as a base class.
Are you confusing _dtor_ with _ctor_?
No, he's confusing two different schemes for ensuring dynamic
allocation: (1) using factory factions, and (2) using protected or
private destructor, where the main advantage is that you /don't/ have to
create a factory function per constructor.

See section 1.1.3 of <url:
http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf>.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 3 '06 #5

P: n/a
* Alf P. Steinbach:
* Shimin:
>Jerry Coffin wrote:
>>In article <11**********************@a14g2000cwb.googlegroups .com>,
da*************@gmail.com says...
I wonder if there is any reasonable reason to make virtual destructor
protected within a derived class purposely?

A virtual protected dtor will normally be accompanied by a static
member function to create objects. The dtor is protected to prevent
outside code from creating instances of the class without using the
static member function to create them. Making it virtual and
protected means this is intended to be used as a base class.
Are you confusing _dtor_ with _ctor_?

No, he's confusing two different schemes for ensuring dynamic
allocation: (1) using factory factions, and (2) using protected or
private destructor, where the main advantage is that you /don't/ have to
create a factory function per constructor.

See section 1.1.3 of <url:
http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf>.
1.3.3, sorry.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 3 '06 #6

P: n/a
thank you all for helping, I'll take a look at the related chapters in
design pattern...

cheers
daniel.

Jul 3 '06 #7

P: n/a
In article <e8**********@rumours.uwaterloo.ca>, sm*******@gmail.com
says...
Jerry Coffin wrote:
In article <11**********************@a14g2000cwb.googlegroups .com>,
da*************@gmail.com says...
I wonder if there is any reasonable reason to make virtual destructor
protected within a derived class purposely?
A virtual protected dtor will normally be accompanied by a static
member function to create objects. The dtor is protected to prevent
outside code from creating instances of the class without using the
static member function to create them. Making it virtual and
protected means this is intended to be used as a base class.
Are you confusing _dtor_ with _ctor_?
No -- the dtor has essentially the same effect. For example, if you
create an object with automatic storage duration, it has to be
destroyed when it goes out of scope. If the dtor is private or
protected, you don't have access to the dtor, so the compiler won't
allow you to create the automatic object.

IOW, you can accomplish essentially the same thing with either the
ctor or the dtor.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 3 '06 #8

P: n/a
thank you all for your great help!
I've found the protected dtor will make an object be created from heap
only, that is: using new operator. from this aspect, the trick is
different from making ctor private or protected, in that way, object
must be created through another member function. I think both are quite
useful within certain contexts.

thanks again..

daniel

Jul 17 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.