473,486 Members | 1,970 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2151
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
11121
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
10487
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
1795
by: Razvan | last post by:
Hi! What is wrong with the following code ? // Test1.cpp : Defines the entry point for the console application. //
37
4118
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
1337
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
2047
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
257
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
1972
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
1885
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
7099
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,...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7319
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3069
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.