473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

what's the trick of making virtual destructor protedted?

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

Jul 3 '06 #1
8 2143
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
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

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
* 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
* 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
thank you all for helping, I'll take a look at the related chapters in
design pattern...

cheers
daniel.

Jul 3 '06 #7
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Dario | last post by:
The following simple program behaves differently in Windows and Linux . #include <stdexcept> #include <iostream> #include <string> using namespace std; class LogicError : public logic_error {...
11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
3
by: Razvan | last post by:
Hi! What is wrong with the following code ? // Test1.cpp : Defines the entry point for the console application. //
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
8
by: Fletcher Glenn | last post by:
I've done some web searching and I cannot find a suitable answer to this question for the following circumstances. Consider: class Base { private: Otherclass* instance; public: Base() {...
3
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual...
0
by: daniel | last post by:
I wonder if there is any reasonable reason to make virtual destructor protected within a derived class purposely? thanks. daniel
7
by: sam | last post by:
Hi, See when i reading a sourcecode of a program, I read that the constructor is ordinary and after that the programmer has written virtual destructor for that constructor . Why we use the...
3
by: GAURAV AGRAWAL | last post by:
Hi Guys, Can someone please explain me why this is happening #include<iostream> using namespace std; class a { public: int a1; // If I remove this it'll work fine
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.