|
OK, I must of missed something here, but when I use a function object in a std::for_each loop, the constructor is called once (as expected), but the destructor is called multiple times! Can someone explain why this happens, and, more importantly, how I can have the destructor called just once. For example:
#include <algorithm>
#include <vector>
#include <iostream>
struct function_object
{
function_object()
{
std::cout << "function_object : " << this << std::endl;
}
~function_object()
{
std::cout << "~function_object : " << this << std::endl;
}
void operator()(int n) const
{
std::cout << n << std::endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
std::vector<int> v;
v.push_back(1);
v.push_back(2);
std::for_each(v.begin(), v.end(), function_object());
return 0;
}
Now, when I run the above app it seems that multiple copies of the function_object struct are created, as the destructor trace code shows different pointers. However, as only one constructor is being called, I am a little confused. The output looks like this:
function_object : 0013FE4B
1
2
~function_object : 0013FE00
~function_object : 0013FE6F
~function_object : 0013FE4B
Any ideas on how to avoid this would be welcome.
Kicking squealing Gucci little piggy.
|
|
|
|
|
OK, so after digging around I realise I need to pass the function object by reference. The only syntax I can get to compile and link seems to be this:
std::vector<int> v;
v.push_back(1);
v.push_back(2);
function_object f;
std::for_each<std::vector<int>::const_iterator, function_object&>(v.begin(), v.end(), f);
Is this correct, 'cos it ain't purdy (OK, so the STL isn't pretty full stop, but...).
Man, the STL - always SO much to learn.
Kicking squealing Gucci little piggy.
|
|
|
|
|
STL passes many things by copy. Thus, while the default constructor is only called once, the copy-constructor may be called many times. To see this, modify your code slightly:
struct function_object
{
function_object()
{
std::cout << "function_object : " << this << std::endl;
}
function_object(const function_object& obj)
{
std::cout << "function_object (copy): " << this << std::endl;
}
~function_object()
{
std::cout << "~function_object : " << this << std::endl;
}
void operator()(int n) const
{
std::cout << n << std::endl;
}
};
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
|
I'm not sure that will even compile, but what exactly are you trying to do? That is, what is your purpose in this question?
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
It compiles. And only one function object is used.
Basically, the constructor of my function object creates a new class (via a boost::smart_ptr), the operator() adds data to the class, and the destructor adds the class to a container (that is passed by reference to the function object constructor). The additional constructors/destructors in my first attempt were obviously causing problems, but passing the function object by reference seems to work. The code would look something like this:
class foo { ... };
...
struct function_object
{
boost::shared_ptr<foo> sp_;
std::vector<boost::shared_ptr<foo> >& vec_;
function_object(std::vector<boost::shared_ptr<foo> >& vec) : vec_(vec), sp_(new foo)
{
}
~function_object()
{
vec_.push_back(sp_);
}
void operator()(int n) const
{
}
};
...
std::vector<boost::shared_ptr<foo> > vec;
std::vector<int> int_vec;
int_vec.push_back(1);
int_vec.push_back(2);
function_object f(vec);
std::for_each<std::vector<int>::const_iterator, function_object&>(int_vec.begin(), int_vec.end(), f);
Kicking squealing Gucci little piggy.
|
|
|
|
|
Purely out of curiousity, what are you doing all that for? That is, what are you using sp_ for that you want 1 created to do everything in the loop?
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Hi All,
what is WTL ? Where it is used ? How can i learn this ?
Kindly help me .
|
|
|
|
|
|
|
Samy_Ibraheem_Mo wrote: I see that WTL is more difficult than MFC in its use as it depend on templates, raw code of win32, and more advanced windows programming config
You're entitled to your opinion, but I disagree. WTL is a cleaner model than MFC, and templates don't make it any more complex than MFC. It does for developers using the UI what ATL does for developers using COM.
You can use WTL without using SDK calls, or if you wish you can use SDK calls, just like in MFC. I don't understand what you mean by 'more advanced windows programming config', so I can't really address that.
However, it is not an MFC clone, it doesn't have built-in document/view, serialization or docking window support.
The best intros to WTL are those on CodeProject, (of course).
WTL should be an essential part of a C++ developer's toolkit if they are considering developing for the Windows UI.
Steve S
Developer for hire
|
|
|
|
|
hi, all:
I've developed a bho plugin for internet explorer. It simply receive the DISPID_BEFORENAVIGATE2 event and change the address in the address bar. It's event-driven and I think it will not cost too much cpu. But to my surprise when it's loaded into IE, the cpu usage turns to be 100% and I can do nothing else.
Why it costs so much cpu and how to resolve this problem?
Thank you very much!
harald
|
|
|
|
|
Hi, guys,
do you know of any WTL code wrappers for Scintilla?
Thanks a lot,
Igor.
|
|
|
|
|
|
|
i create a com dll using an ATL and attribute and i try to register this file when i call LoadLibrary( DllPath ) i got a null pointer and i don't know why.
some computer print this message box "the application failed to start because MSVCR70D.dll component not found"
can i make a attributed ATL7 com dll independant on any other components
-- modified at 16:13 Sunday 24th September, 2006
|
|
|
|
|
i try to build the project in release configuration and the problem disappear
|
|
|
|
|
When you compile in debug configuration, it links with MSVCR70D.dll instead of MSVCR70.dll
MSVCR70D.dll is the debug configuration dll of vc runtime.
To solve this problem, all you need is the MSVCR70D.dll on the box where you are trying to register your Atl7 COM server.
S o h a i l K a d i w a l a
To Err Is Human; to Debug, Divine
modified 21-Apr-21 21:01pm.
|
|
|
|
|
You can remove that dependency but going to Ptoject/Properties, go to Code generation and selecte "Mulithreaded Debug" instead of "Multithareded Debug DLL"
|
|
|
|
|
I need to learn fast about the creation of ATL Composite Control Projects.
Where could I find abundant information about this??.
Maybe a tutorial in the WEB.
Maybe a book.
I have read a lot about ATL projects but I need something specific about Composite Controls. Something that teach me how should I move inside this kind of projects.
I have some good skills with MFC and I'm learning general ATL stuff.
Thank you very much.
Demian.
Demian.
"I have always wished that my computer would be as easy to use as my
telephone. My wish has come true. I no longer know how to use my telephone."
-Bjarne Stroustrup, computer science professor, designer of C++
programming language (1950- )
|
|
|
|
|
Just use the composite control wizard. The composite control is basically a non-modal dialog. I suggest you use WTL for the GUI-stuff, as it makes your life a whole lot easier.
--
This episode performed entirely by sock puppets
|
|
|
|
|
Hi.
I have a WTL 7.5 app created in VS2005 on XP.
In the app I was intending to make use of various STL containers. I began by entering:
using namespace std;
just under my header declarations, BOOM!
I instantly got 11 errors, 5 pairs of:
error C2006: '#using' : expected a filename, found 'namespace'
fatal error C1190: managed targeted code requires a '/clr' option
and 1:
BSCMAKE: error BK1506 : cannot open file '.\Debug\DispInterface.sbr': No such file or directory.
I've rummaged around my settings and could see nothing that cause this (though obviously, I expect I'm missing something). Anyway, I continued with the app as I could use std:: to use the containers I wanted, only to find when I called push_back on a vector I got a heap corruption (C0000005).
I can only presume these are connected as I understand neither.
Any insights would be greatly appreciated
Thanks
Tom
|
|
|
|
|
Weird - it seems to be parsing using as #using !
How about using the std namespace qualifier directly, e.g.:
std::string str = "Test";
Does this work OK?
FWIW I use the STL with all my WTL/ATL apps with no issues, however, I avoid using namespace ... . I prefer to specify the namespace directly.
Should I use using namespace std in my code?[^]
Kicking squealing Gucci little piggy.
|
|
|
|
|
Rob Caldecott wrote: I avoid using namespace
And me - the only namespace I use is boost::lambda , and then only in the smallest scope I can find.
|
|
|
|
|
TClarke wrote: #using
'#using' - sure there's not a rogue '#' in there?
Also - I've used STL and WTL together with *no* problems, so I'd suggest it isn't going to be that...
|
|
|
|