C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

what's the trick of making virtual destructor protedted?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
daniel
Guest





PostPosted: Mon Jul 03, 2006 8:29 am    Post subject: what's the trick of making virtual destructor protedted? Reply with quote



I wonder if there is any reasonable reason to make virtual destructor
protected within a derived class purposely?
Back to top
Jerry Coffin
Guest





PostPosted: Mon Jul 03, 2006 9:03 am    Post subject: Re: what's the trick of making virtual destructor protedted? Reply with quote



In article <1151897380.688686.137500 (AT) a14g2000cwb (DOT) googlegroups.com>,
daniel.huangfei (AT) gmail (DOT) com says...
Quote:
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.
Back to top
Shimin
Guest





PostPosted: Mon Jul 03, 2006 9:10 am    Post subject: Re: what's the trick of making virtual destructor protedted? Reply with quote



Jerry Coffin wrote:
Quote:
In article <1151897380.688686.137500 (AT) a14g2000cwb (DOT) googlegroups.com>,
daniel.huangfei (AT) gmail (DOT) 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
Back to top
Alf P. Steinbach
Guest





PostPosted: Mon Jul 03, 2006 9:10 am    Post subject: Re: what's the trick of making virtual destructor protedted? Reply with quote

* Shimin:
Quote:
Jerry Coffin wrote:
In article <1151897380.688686.137500 (AT) a14g2000cwb (DOT) googlegroups.com>,
daniel.huangfei (AT) gmail (DOT) 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?
Back to top
daniel
Guest





PostPosted: Mon Jul 03, 2006 9:10 am    Post subject: Re: what's the trick of making virtual destructor protedted? Reply with quote

Jerry Coffin wrote:
Quote:
In article <1151897380.688686.137500 (AT) a14g2000cwb (DOT) googlegroups.com>,
daniel.huangfei (AT) gmail (DOT) 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
Quote:
Later,
Jerry.

The universe is a figment of its own imagination.
Back to top
Alf P. Steinbach
Guest





PostPosted: Mon Jul 03, 2006 9:10 am    Post subject: Re: what's the trick of making virtual destructor protedted? Reply with quote

* Alf P. Steinbach:
Quote:
* Shimin:
Jerry Coffin wrote:
In article <1151897380.688686.137500 (AT) a14g2000cwb (DOT) googlegroups.com>,
daniel.huangfei (AT) gmail (DOT) 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?
Back to top
daniel
Guest





PostPosted: Mon Jul 03, 2006 9:10 am    Post subject: Re: what's the trick of making virtual destructor protedted? Reply with quote

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

cheers
daniel.
Back to top
Jerry Coffin
Guest





PostPosted: Mon Jul 03, 2006 9:10 am    Post subject: Re: what's the trick of making virtual destructor protedted? Reply with quote

In article <e8aah3$4qg$1 (AT) rumours (DOT) uwaterloo.ca>, smguo2001 (AT) gmail (DOT) com
says...
Quote:
Jerry Coffin wrote:
In article <1151897380.688686.137500 (AT) a14g2000cwb (DOT) googlegroups.com>,
daniel.huangfei (AT) gmail (DOT) 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.
Back to top
daniel
Guest





PostPosted: Mon Jul 17, 2006 7:23 am    Post subject: Re: what's the trick of making virtual destructor protedted? Reply with quote

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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.