Functors with behaviour in the destructor

Discussion in 'C++' started by Paul MG, Jul 2, 2003.

  1. Paul MG

    Paul MG Guest

    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:)
     
    Paul MG, Jul 2, 2003
    #1
    1. Advertising

  2. Paul MG

    Cy Edmunds Guest

    "Paul MG" <> wrote in message
    news:...
    > 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/
     
    Cy Edmunds, Jul 2, 2003
    #2
    1. Advertising

  3. Paul MG

    Dhruv Guest

    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.
     
    Dhruv, Jul 3, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. frs
    Replies:
    20
    Views:
    776
    Alf P. Steinbach
    Sep 21, 2005
  2. LAvoisieR
    Replies:
    8
    Views:
    439
    LAvoisieR
    Oct 27, 2005
  3. arun
    Replies:
    2
    Views:
    554
    benben
    Jun 13, 2006
  4. Jimmy Hartzell
    Replies:
    0
    Views:
    426
    Jimmy Hartzell
    May 19, 2008
  5. Jimmy Hartzell
    Replies:
    2
    Views:
    1,175
    Jimmy Hartzell
    May 20, 2008
Loading...

Share This Page