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

refactoring a destructor

I am trying to pull behavior out of a class so that the class can be
replaced. Part of that behavior takes place in the destructor of the
object. I need call sites to change so that they call a cleaner
function and then let the object get deleted normally.

Any good ideas on a nice approach? Best I can seem to come up with is
to privatize the destructor and let the compiler complain. Problem is
that it goes a little insain and I think comes up with a lot of false
positives...or extra locations...

Nov 23 '06 #1
3 1236
"Noah Roberts" <ro**********@gmail.comwrote:
I am trying to pull behavior out of a class so that the class can be
replaced. Part of that behavior takes place in the destructor of the
object. I need call sites to change so that they call a cleaner
function and then let the object get deleted normally.

Any good ideas on a nice approach? Best I can seem to come up with is
to privatize the destructor and let the compiler complain. Problem is
that it goes a little insain and I think comes up with a lot of false
positives...or extra locations...
That's a tough one. If you make the d_tor private, the compiler will
complain at object construction, if you remove the d_tor the compiler
will provide one. :-(

Unless the objects of this class are always newed and deleted, I
question the wisdom of removing the class RAII demands the object exist.
I'd say do one of two things, either refactor the class itself so that
the default d_tor is adequate (effectively removing the need to refactor
the d_tor directly,) or comment out Class* declarations to flush out all
the "delete object" references where 'object' is of type Class* and put
your new function just above each delete.

--
To send me email, put "sheltie" in the subject.
Nov 23 '06 #2

Daniel T. wrote:
"Noah Roberts" <ro**********@gmail.comwrote:
I am trying to pull behavior out of a class so that the class can be
replaced. Part of that behavior takes place in the destructor of the
object. I need call sites to change so that they call a cleaner
function and then let the object get deleted normally.

Any good ideas on a nice approach? Best I can seem to come up with is
to privatize the destructor and let the compiler complain. Problem is
that it goes a little insain and I think comes up with a lot of false
positives...or extra locations...

That's a tough one. If you make the d_tor private, the compiler will
complain at object construction, if you remove the d_tor the compiler
will provide one. :-(

Unless the objects of this class are always newed and deleted, I
question the wisdom of removing the class RAII demands the object exist.
I'd say do one of two things, either refactor the class itself so that
the default d_tor is adequate (effectively removing the need to refactor
the d_tor directly,) or comment out Class* declarations to flush out all
the "delete object" references where 'object' is of type Class* and put
your new function just above each delete.
I'm going to cut this corner. The principles surrounding refactoring
say you should refactor one step at a time but in this case I will need
to do two. I'm working on removing an invasive list structure that has
caused long years of feature creep in unrelated but sibling classes. I
want to make it look and act like any old stl adt but this one owns its
pointers....deletes them when it is destroyed and such.

I'm going to have to switch ADT and pointer type at the same time so
that I make the switch to STL containers and use shared_ptr
simultaneously hoping that they come close to behavior enough to make
it work. Obviously anywhere that this list deleted its pointers will
also hapen with shared_ptrs (assuming they aren't kept elsewhere), and
anywhere that is holding raw copies still needs to be sure that the
list exists so there should be little trouble. Unfortunately I'm in a
legacy situation and have no harness...

I thought maybe I could privatize the destructor and call a destroy
function on the class (that would necissarily have to be a
friend)...unfortunately the primary use of this class is stack based so
that's out. I'm kind of painted into a corner but I think I can weazel
my way out.

Nov 25 '06 #3
"Noah Roberts" <ro**********@gmail.comwrote:
Daniel T. wrote:
>"Noah Roberts" <ro**********@gmail.comwrote:
>>I am trying to pull behavior out of a class so that the class can
be replaced. Part of that behavior takes place in the destructor
of the object. I need call sites to change so that they call a
cleaner function and then let the object get deleted normally.

Any good ideas on a nice approach? Best I can seem to come up
with is to privatize the destructor and let the compiler complain.
Problem is that it goes a little insain and I think comes up with
a lot of false positives...or extra locations...

That's a tough one. If you make the d_tor private, the compiler
will complain at object construction, if you remove the d_tor the
compiler will provide one. :-(

Unless the objects of this class are always newed and deleted, I
question the wisdom of removing the class RAII demands the object
exist. I'd say do one of two things, either refactor the class
itself so that the default d_tor is adequate (effectively removing
the need to refactor the d_tor directly,) or comment out Class*
declarations to flush out all the "delete object" references where
'object' is of type Class* and put your new function just above
each delete.

I'm going to cut this corner. The principles surrounding
refactoring say you should refactor one step at a time but in this
case I will need to do two. I'm working on removing an invasive
list structure that has caused long years of feature creep in
unrelated but sibling classes. I want to make it look and act like
any old stl adt but this one owns its pointers....deletes them when
it is destroyed and such.

I'm going to have to switch ADT and pointer type at the same time so
that I make the switch to STL containers and use shared_ptr
simultaneously hoping that they come close to behavior enough to
make it work. Obviously anywhere that this list deleted its
pointers will also hapen with shared_ptrs (assuming they aren't kept
elsewhere), and anywhere that is holding raw copies still needs to
be sure that the list exists so there should be little trouble.
Unfortunately I'm in a legacy situation and have no harness...

I thought maybe I could privatize the destructor and call a destroy
function on the class (that would necissarily have to be a
friend)...unfortunately the primary use of this class is stack based
so that's out. I'm kind of painted into a corner but I think I can
weazel my way out.
I would first refactor the invasive list to use share_ptr.

--
To send me email, put "sheltie" in the subject.
Nov 25 '06 #4

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

Similar topics

9
by: Peter Dembinski | last post by:
I am trying to write Master Thesis on refactoring Python code. Where should I look for information? -- http://www.dembiński.prv.pl
4
by: | last post by:
Hi, Refactoring a winform causes problems if moving this form to a subdirectory in the same project. What is the workaround for this and will this be fixed in future? Thanks /BOB
0
by: Andre Baresel | last post by:
Hello together, just a year ago I was searching arround for a tool supporting refactoring for c++. I've seen implementations for java and was impressed how an IDE can help with such a feature....
2
by: Sachin Garg | last post by:
Hi, I was trying to find (like many others here) a tool for refactoring C++ code as I have lately been noticing that I spend most of my coding time doing refactoring and some refactoring which...
6
by: Dean Ware | last post by:
Hi, I am part way through developing a C++ application. I am developing it on my own and using VC++ 6.0. I am now at the stage where I wish to start tidying up my code. I have lots of...
8
by: Frank Rizzo | last post by:
I keep hearing this term thrown around. What does it mean in the context of code? Can someone provide a definition and example using concrete code? Thanks.
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
15
by: Simon Cooke | last post by:
Does anyone know of any tools for refactoring header files? We're using a third party codebase at work, and pretty much every file includes a 50Mb precompiled header file. I'm looking for a tool...
2
by: HendrikLeder | last post by:
Hello everybody :-) Next year I`ll write my diploma in computer science for business (It`s a degree in Germany) and I`ve some questions about the topic. The diploma will handle about refactoring...
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
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
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
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,...
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...
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,...

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.