473,387 Members | 1,504 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,387 software developers and data experts.

c++ language design teaser

Given a pointer within an object, C++ runtime could have been written
to delete the pointed object automatically. Why is it that they
require a destructor to be written instead?
Jul 22 '05 #1
8 1189

"Rajat Chadda" <ra**********@wipro.com> wrote
Given a pointer within an object, C++ runtime could have been written to delete the pointed object automatically. Why is it that they
require a destructor to be written instead?


Not all pointers point to dynamically created objects. Not all
dynamically created objects are created with new. Not all objects
containing pointers (even pointers to dynamically created objects)
own the pointed-to object. That's just off the top of my head.

Hope that helps,
Buster.
Jul 22 '05 #2
Buster wrote:
"Rajat Chadda" <ra**********@wipro.com> wrote
Given a pointer within an object, C++ runtime could have been written
to delete the pointed object automatically. Why is it that they
require a destructor to be written instead?


Not all pointers point to dynamically created objects. Not all
dynamically created objects are created with new. Not all objects
containing pointers (even pointers to dynamically created objects)
own the pointed-to object. That's just off the top of my head.


Good answer. I would add that if one doesn't want (or is too lazy) to
write a destructor, he/she can just use std::auto_ptr or any other smart
pointer class instead of using a bare pointer.

Alberto
Jul 22 '05 #3
Alberto Barbati wrote:

I would add that if one doesn't want (or is too lazy) to
write a destructor, he/she can just use std::auto_ptr or any other smart
pointer class instead of using a bare pointer.


Be careful: using auto_ptr also gives specific meanings to the copy
constructor and assigment operator, and those are usually not what was
intended.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #4
"Buster" <no***@nowhere.com> wrote in message
news:c1**********@newsg2.svr.pol.co.uk...

"Rajat Chadda" <ra**********@wipro.com> wrote
Given a pointer within an object, C++ runtime could have been

written
to delete the pointed object automatically. Why is it that they
require a destructor to be written instead?


Not all pointers point to dynamically created objects. Not all
dynamically created objects are created with new. Not all objects
containing pointers (even pointers to dynamically created objects)
own the pointed-to object. That's just off the top of my head.

Hope that helps,
Buster.


Good list. I will also add that destructors don't necessarily have anything
to do with deleting pointers. They can free other resources (e.g. files) or
do other kinds of cleanup activities which have nothing to do with releasing
resources.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #5
Rajat Chadda wrote:
Given a pointer within an object, C++ runtime could have been written
to delete the pointed object automatically. Why is it that they
require a destructor to be written instead?


Because in many cases the object pointed by the pointer is not supposed
to be deleted with the object that stores the pointer.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #6
Rajat Chadda wrote:
Given a pointer within an object, C++ runtime could have been written
to delete the pointed object automatically. Why is it that they
require a destructor to be written instead?


All of the answers I heard are very solid, however, I think it might
have more to do with bindings to C programming paradigms and design than
all the other commentary.

I am pretty sure that there are countless solutions to this problem,
however, the requirement of backward compatibility with C makes this
more complex than originally intended. Furthermore, it makes C++ more
disimilar to C, which might have been considered a bad thing.

Comments?

JLR
Jul 22 '05 #7

"Jorge Rivera" <jo*****@rochester.rr.com> wrote in message
news:bQ*******************@twister.nyroc.rr.com...
Rajat Chadda wrote:
Given a pointer within an object, C++ runtime could have been written
to delete the pointed object automatically. Why is it that they
require a destructor to be written instead?


All of the answers I heard are very solid, however, I think it might
have more to do with bindings to C programming paradigms and design than
all the other commentary.

I am pretty sure that there are countless solutions to this problem,
however, the requirement of backward compatibility with C makes this
more complex than originally intended. Furthermore, it makes C++ more
disimilar to C, which might have been considered a bad thing.

Comments?


Yes. You wrote above:

"I am pretty sure that there are countless solutions to this problem"

OP did not describe any problem.

-Mike
Jul 22 '05 #8
Pete Becker wrote:
Alberto Barbati wrote:
I would add that if one doesn't want (or is too lazy) to
write a destructor, he/she can just use std::auto_ptr or any other smart
pointer class instead of using a bare pointer.


Be careful: using auto_ptr also gives specific meanings to the copy
constructor and assigment operator, and those are usually not what was
intended.


Good you noticed. Generally speaking, std::auto_ptr is ok for classes
that are not going to be copied/assigned. When an std::auto_ptr is
around, forbidding the copy ctor and assignment operator is usually a
good thing, unless you know what you're doing. Alternatively, one could
avoid std::auto_ptr in favor of boost::scoped_ptr, which has exactly the
right semantic (i.e.: it invokes delete on destruction but it's
non-copiable).

If the class is going to be copied and/or assigned, other smart pointer
classes, for example boost::shared_ptr, are usually a better choice.

Alberto
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: brendan | last post by:
here's a brain teaser for someone with more skills or at least more lateral thinking capability than me - done my nut over this one... have written a list manager in PHP which a) posts out new...
49
by: Ville Vainio | last post by:
I don't know if you have seen this before, but here goes: http://text.userlinux.com/white_paper.html There is a jab at Python, though, mentioning that Ruby is more "refined". -- Ville...
26
by: Mark Hahn | last post by:
This is an announcement of the beginning of development of a new Python-like language called PyCs (pronounced "pie-cees"). Like IronPython, PyCs will be Python on .Net but it will have more...
72
by: Mel | last post by:
Are we going backwards ? (please excuse my spelling...) In my opinion an absolute YES ! Take a look at what we are doing ! we create TAGS, things like <H1> etc. and although there are tools...
70
by: KingIshu | last post by:
Hi All, I am developing an object oriented OS code-named "ikbocs". 1) What are the pros and cons of the following languages interms of Effectiveness on System Programming, Object Orientedness etc...
15
by: Chung Leong | last post by:
Here's a little brain teaser distilled from a bug that took me a rather long time to figure out. The two functions in the example below behave differently. The difference is easy to spot, of...
11
by: Bob Rosen | last post by:
I have used VB.NET for several months. Just recently I was asked to modify some web pages that were developed with C#. Although I know that C# is much more popular, in comparing the two all of...
2
RedSon
by: RedSon | last post by:
Given a directed graph of k-nodes such that the last node creates a cycle with any other node determine which node the last node's edge points to using the minimum amount of resources and without...
27
by: notnorwegian | last post by:
(might not be the right forum for this but...) what is the definition of a highlevel-language? well there isnt one specifically and wikipedia and the like gives just a very general description...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.