472,789 Members | 1,329 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 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 2108
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.