473,397 Members | 2,099 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,397 software developers and data experts.

deleting the referent

Hi. Is there anything to be wary of when delete-ing the object to which a
reference refers? Other than being certain that the reference won't be used
again? Notwithstanding the contrived example below, is this really a
horrible thing to do, to the extent that I should avoid creating the
reference in the first place? In my actual code, B::rr is just a convenience
used during the lifetime of B.

Thanks,
- Jeff.

class R;
class B {
R& rr;
public:
B(R& r) : rr(r) {};
~B() { delete &rr; do_other_stuff_in_B(this); };
};

int main(() {
R* pr = new R;
B b(*pr);
}
Jul 19 '05 #1
3 1617
Jeff Rosenfeld <sp********@comcast.net> wrote in message
news:UQ********************@comcast.com...
Hi. Is there anything to be wary of when delete-ing the object to which a
reference refers? Other than being certain that the reference won't be used again? Notwithstanding the contrived example below, is this really a
horrible thing to do, to the extent that I should avoid creating the
reference in the first place?


It's not very common as far as I know. It does look a little strange, but I
don't think you can make a blanket statement about it.

I did manage to find some old code of mine that has such a reference:

CompositeGraphResource::CompositeGraphResource()
: GraphResource(GC_RES_NULL),
m_resourcePtrList(*new GraphResourcePtrList)
{
}

CompositeGraphResource::CompositeGraphResource(con st CompositeGraphResource
&r)
: GraphResource(r), m_resourcePtrList(*new
GraphResourcePtrList(r.m_resourcePtrList))
{
}

CompositeGraphResource::~CompositeGraphResource()
{
delete &m_resourcePtrList;
}

I don't remember why I did it. I guess it was just because I knew I would
never replace the referred-to object, and I must have thought that I'd
rather use . everywhere than ->. It beats me why I bothered using a
reference _or_ a pointer. Why not just make m_resourcePtrList a member
object? Anyway, for private use within a class like this I don't see a
problem.

I find that pointers look like things that might need deleting, but
references don't, so I guess I'd probably avoid it where it's not as local
as it was in my case. Maybe some people like using references for neatness
wherever possible and don't have any objection to it.

David

Jul 19 '05 #2
"Jeff Rosenfeld" <sp********@comcast.net> wrote in message news:<UQ********************@comcast.com>...
Hi. Is there anything to be wary of when delete-ing the object to which a
reference refers? Other than being certain that the reference won't be used
again? Notwithstanding the contrived example below, is this really a
horrible thing to do, to the extent that I should avoid creating the
reference in the first place? In my actual code, B::rr is just a convenience
used during the lifetime of B.

Thanks,
- Jeff.

class R;
class B {
R& rr;
public:
B(R& r) : rr(r) {};
~B() { delete &rr; do_other_stuff_in_B(this); };
};

int main(() {
R* pr = new R;
B b(*pr);
}


This is absolutely horrible. For a start, how can you tell if rr was
allocated using new? Your class has no evidence whatsoever that this
is so, thus verifying the correctness of B requires you to examine
every usage of it.
Even so, using a reference makes no sense at all. A reference does not
convey any hint that the refered to object could possibly be allocated
using new.

The solution: Do not delete the object in B but outside. Then you can
use new whenever you need so and automatic, faster and safer storage
in situations where this is feasible - such as main above.

Kind regards
Peter
Jul 19 '05 #3
Fair enough, but I'm not doing this as part of a library. In fact, the
equivalent to B in my case exists for as long as it takes to decide which
one of two R's is going to be deleted. The B goes away shortly after one of
the R's does. B is representative of a system state, not so much a block of
data. In fact, other parts of the system alter their behaviors when B exists
and there is never more than one B at a time.

I suppose it's possible to isolate B by passing its constructor an object
that describes the post-B action to take. That would also require that
something outside of B itself could be alerted to B's destruction (not
currently the case; B invokes delete on itself because it is the only one
that knows when its decision is made). I tried a little too hard to simplify
my example.

- Jeff.

"Peter Koch Larsen" <pk*@mailme.dk> wrote in message
news:61**************************@posting.google.c om...
"Jeff Rosenfeld" <sp********@comcast.net> wrote in message

news:<UQ********************@comcast.com>...
Hi. Is there anything to be wary of when delete-ing the object to which a reference refers? Other than being certain that the reference won't be used again? Notwithstanding the contrived example below, is this really a
horrible thing to do, to the extent that I should avoid creating the
reference in the first place? In my actual code, B::rr is just a convenience used during the lifetime of B.

Thanks,
- Jeff.

class R;
class B {
R& rr;
public:
B(R& r) : rr(r) {};
~B() { delete &rr; do_other_stuff_in_B(this); };
};

int main(() {
R* pr = new R;
B b(*pr);
}


This is absolutely horrible. For a start, how can you tell if rr was
allocated using new? Your class has no evidence whatsoever that this
is so, thus verifying the correctness of B requires you to examine
every usage of it.
Even so, using a reference makes no sense at all. A reference does not
convey any hint that the refered to object could possibly be allocated
using new.

The solution: Do not delete the object in B but outside. Then you can
use new whenever you need so and automatic, faster and safer storage
in situations where this is feasible - such as main above.

Kind regards
Peter

Jul 19 '05 #4

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

Similar topics

5
by: flupke | last post by:
Hi, i'm having trouble with deleting elements from a list in a for loop ============== test program ============== el = print "**** Start ****" print "List = %s " % el index = 0 for line...
15
by: Rick | last post by:
Hi, Does deleting an object more than one times incur undefined behavior? I think it doesn't but just making sure... thanks Rick
6
by: Abhijeet | last post by:
I was just toying around idea of deleting this from a member function. Was expecting that any acess to member variable or function after deleting sould give me dump(segmetation violation).Cause now...
6
by: Matan Nassau | last post by:
Hello. i have a composite which i want to delete. this is a composite which represents a boolean expression (see a previous post of mine with more details at...
3
by: Nathan Bloom | last post by:
Hi, I have a data entry form (access 2000) that also allows the user to add, update, and delete records from the form. The Delete action is carried out in an event procedure and has the...
4
by: al havrilla | last post by:
hi all what does the phrase: "scalar deleting destructor" mean? i'm getting this in a debug error message using c++ 7.1 thanks Al
6
by: Martin Bischoff | last post by:
Hi, I'm creating temporary directories in my web app (e.g. ~/data/temp/temp123) to allow users to upload files. When I later delete these directories (from the code behind), the application...
3
by: Kimera.Kimera | last post by:
I'm trying to write a program in VB.net 2003 that basically deletes all files, folders, sub-folders and sub-sub folders (etc). The program is simply for deleting the Windows/Temp folder contents,...
2
Parul Bagadia
by: Parul Bagadia | last post by:
I have written a code for deleting certain value from linklist; it's not working; where as i have written one for deleting a no., after given no. which works fine! I even debugged it; but invain;...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
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
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
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.