Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Functors with behaviour in the destructor

Reply
Thread Tools

Functors with behaviour in the destructor

 
 
Paul MG
Guest
Posts: n/a
 
      07-02-2003
Hi

A brief question:

Is it generally considered idiomatic/good C++ to have behaviour in the
destructor of a function object?

For instance, consider a function object whose constructor takes
references to variables in the calling scope. During the use of the
functor, ie in a call to an STL algorithm, the functor stores away
data in its member variables. Then just before the algorithm returns,
as the functor goes out of scope, its destructor does some work with
the member variables, setting the results back into the variables it
still has references to.

I hope this makes sense. If it is any use, I am seeing this pattern at
http://www.gotw.ca/gotw/041.htm. I just found it rather surprising,
though once I had read it I sort of saw the elegance of it.

Is this a standard thing to do?

thanx for input,

pmg
 
Reply With Quote
 
 
 
 
Cy Edmunds
Guest
Posts: n/a
 
      07-02-2003
"Paul MG" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Hi
>
> A brief question:
>
> Is it generally considered idiomatic/good C++ to have behaviour in the
> destructor of a function object?
>
> For instance, consider a function object whose constructor takes
> references to variables in the calling scope. During the use of the
> functor, ie in a call to an STL algorithm, the functor stores away
> data in its member variables. Then just before the algorithm returns,
> as the functor goes out of scope, its destructor does some work with
> the member variables, setting the results back into the variables it
> still has references to.
>
> I hope this makes sense. If it is any use, I am seeing this pattern at
> http://www.gotw.ca/gotw/041.htm. I just found it rather surprising,
> though once I had read it I sort of saw the elegance of it.
>
> Is this a standard thing to do?
>
> thanx for input,
>
> pmg


As Edward Asner once said on the Mary Tyler Moore show: "It's cute. I hate
cute."

Doing real work as a side effect of a destructor is OK for a puzzle but I
would prefer a less whimsical design for industrial strength code.

--
Cy
http://home.rochester.rr.com/cyhome/


 
Reply With Quote
 
 
 
 
Dhruv
Guest
Posts: n/a
 
      07-03-2003
There's a post somewhere around here that says:
std::for_each makes 3 copies of the function object passed to it. Is that
enough to convince you not to use such a tactic, unless of course, you
have a reference counted function object. But, then again, it will give
problems. If the objects upon which the algotihm works itself calls some
other function, to which the obeject is passed, and you expect the
destructor to be called for those individual data sets, then, you cannot
do it. It (the dtor) will be called when the last function that used the
object has exited.

-Dhruv.






 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 2 05-20-2008 02:20 AM
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 0 05-19-2008 07:05 PM
compiler generated destructor vs class implemented destructor arun C++ 2 06-13-2006 05:43 AM
curious behaviour in virtual destructor inside base class LAvoisieR C++ 8 10-27-2005 07:40 PM
Explicit destructor calls from inside base class destructor frs C++ 20 09-21-2005 09:22 AM



Advertisments