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

Functors with behaviour in the destructor

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:)
Jul 19 '05 #1
2 2303
"Paul MG" <pa****@digitalbrain.com> wrote in message
news:26**************************@posting.google.c 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/
Jul 19 '05 #2
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.


Jul 19 '05 #3

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

Similar topics

0
by: red floyd | last post by:
Disclaimer: VS.NET 2003 (haven't checked any other compiler). I'm writing functors for my classes. Because the objects in my containers are large, I'm making my functors take const T& parameters....
2
by: nsgi_2004 | last post by:
Hi, I have been learning about functors and at first everything was clear. Make a class and overload operator () so that an object of the functor can be thought of as a function. However, I...
8
by: LAvoisieR | last post by:
Following test code behaviour suprised me and I dont know what's wrong with this. I have two overloaded constructors within base class and virtual destructor implemented. Derived class uses...
8
by: Daniel Yelland | last post by:
Hi, I have developed a number of code libraries in Win32 DLLs and have written a number of test suite executables that implicitly link to these libraries in order to test them. In one of my test...
5
by: dragoncoder | last post by:
Consider the following code. Does it produce undefined behaviour because destructor of A is not virtual ? #include<iostream> using namespace std; class A { public: A() {
4
by: tryptik | last post by:
Hello all, I have a question about iterators. I have a container of functors that operate on an std::string. The functors go something like this: class Functor { std::string...
7
by: dragoncoder | last post by:
Hello experts, I was just playing around wrote this code. sundev1:/home/ptiwary/rnd $ cat a1.cpp #include <iostream> using namespace std; class Base
2
by: Jon Slaughter | last post by:
I'm trying to mess with functors and the way I want it to work is that when I create a functor it will automatically add itself to an array. The attached code demonstrates what I mean. The...
4
by: Christopher | last post by:
I used to just use a plain old function pointer is a call to std::sort. My colleagues are telling me that I need to use a "functor". Ok, I google and see that a functor is a class with a public...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
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
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...

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.