PAGE2GO2 HOME | INTERNET NEWS

LeighExchange - Free Advertising Network Stock Research at Internet Speed Need Money Easy and Quick?

Re: again the problem: the destructor is called twice

 List
Subject: Re: again the problem: the destructor is called twice
Poster: Jim Langston
Date: Fri, 23 Mar 2007 12:36:58 MST
Related Postings: 1 2 3 4 5 6 7 8 9
"David" wrote in message news:1174678012.543530.293380@l75g2000hse.googlegroups.com...
> On Mar 23, 3:10 pm, "mlimber" wrote:
>> On Mar 23, 11:57 am, rpbg...@yahoo.com (Roland Pibinger) wrote:
>>
>> > On 23 Mar 2007 08:23:01 -0700, "mlimber" wrote:
>> > >You could pass ownership of the test object rather than making a copy.
>> > >One way to do that is to use std::auto_ptr, which signifies unique
>> > >ownership. So if your "call" class accepts an auto_ptr, the
>> > >implicit understanding is that it takes sole ownership of that object.
>>
>> > Goodness gracious! tt is a local automatic object. No need to "pass
>> > ownership" or use a 'smart pointer'. Better make the copy constructor
>> > and operator= for all classes private (leave them un-implemented) and
>> > your code will work with some changes.
>>
>> Like I said, there are multiple ways to accomplish this, and passing
>> ownership like I described is one common and useful way to do it.
>> Compare this FAQ by the Creator on using auto_ptr to pass ownership:
>>
>> http://www.research.att.com/~bs/bs_faq2.html#memory-leaks
>>
>> Of course, the "Big Three" (or probably better, the "Big
>> Two" --http://www.artima.com/cppsource/bigtwo.html) problems
>> fixed in any case.
>>
>> Cheers! --M
>
> Thanks all for your help. I think I need a "deep copy" in my
> copyconstructor. sorry I am new in C++, so when I tried to do below,
> there was something wrong with this:
> test::test(const test& mytest)
> {
>
> map::iterator ii;
>
> for(ii=mytest.tests.begin();ii!=mytest.tests.end();++ii)
> {
> myclass* my=new myclass;
> memcpy(my,ii->second,sizeof(ii->second));
> tests.insert(make_pair(ii->first,my));
> }
>
>
> }
>
> there was an error: "c:\Ug\Solution1\test\test.cpp(16): error C2679:
> binary '=' : no operator found which takes a right-hand operand of
> type 'std::_Tree<_Traits>::const_iterator' " at the for loop point.
> can anybody tell me why and what's the right way to do the copy
> constructor? thanks

The parameter to the copy constructor is a const test& mytest. mytest is constant. You are trying to use a non-constant iterator on it, which isn't allowed. Try:

map::const_iterator ii;

 

Page2Go2.com is not responsible for content of this message.