From: Jack on
std::auto_ptr<BFThread> thread1;

case WM_CREATE:
thread1 = new BFThread(1, hEvent);
break;


////////////
I know I should have used BFThread*
But how do I take advantage of auto_ptr in this example?
I put a direct assignment inside the switch-case statement, but
it results in


From: Jack on
> std::auto_ptr<BFThread> thread1;
>
> case WM_CREATE:
> thread1 = new BFThread(1, hEvent);
> break;
>
>
> ////////////
> I know I should have used BFThread*
> But how do I take advantage of auto_ptr in this example?
> I put a direct assignment inside the switch-case statement, but
> it results in

Here comes the unfinished story.
..... It results in "Error 1 error C2360: initialization of 'thread1' is
skipped by 'case' label"
It was the original statement I coped from, and I wanted to keep it intact.
Anyway, I need to break it up into 2 statements inside wndproc.
What is the correct statement in this case?
Thanks
Jack


From: Alex Blekhman on
On 12-Apr-10 13:48, Jack wrote:
>> std::auto_ptr<BFThread> thread1;
>>
>> case WM_CREATE:
>> thread1 = new BFThread(1, hEvent);
>> break;
>>
>>
>> ////////////
>> I know I should have used BFThread*
>> But how do I take advantage of auto_ptr in this example?
>> I put a direct assignment inside the switch-case statement, but
>> it results in "Error 1 error C2360: initialization of 'thread1' is
> skipped by 'case' label"
> It was the original statement I coped from, and I wanted to keep it intact.
> Anyway, I need to break it up into 2 statements inside wndproc.
> What is the correct statement in this case?

I don't think any statement that involves std::auto_ptr in your case
will be correct. The moment std::auto_ptr instance goes out of scope the
BFThread will be deleted. May be this is what you want, I don't know,
but it seems that you'd like to preserve BFThread instance for the
lifetime of the running thread.

So, if my assumption is right, then you cannot use std::auto_ptr here.
You need to store a pointer to BFThread object somewhere until your
thread exits and only then delete the object. Maybe
std::tr1::shared_pointer, which is stored outside of the function is
more appropriate here.

Alex
From: Jack on
> I don't think any statement that involves std::auto_ptr in your case will
> be correct. The moment std::auto_ptr instance goes out of scope the
> BFThread will be deleted. May be this is what you want, I don't know, but
> it seems that you'd like to preserve BFThread instance for the lifetime of
> the running thread.

Hi Alex,
Thanks for the reply.
Does that mean objects declared with std::auto_ptr only live once and simply
be gone and
can't be reused? If so, I catch your points.
Thanks
Jack



From: Ulrich Eckhardt on
Jack wrote:
> std::auto_ptr<BFThread> thread1;
>
> case WM_CREATE:
> thread1 = new BFThread(1, hEvent);
> break;
>
>
> ////////////
> I know I should have used BFThread*
> But how do I take advantage of auto_ptr in this example?
> I put a direct assignment inside the switch-case statement, but
> it results in "Error 1 error C2360: initialization of 'thread1' is
> skipped by 'case' label"
> It was the original statement I coped from, and I wanted to keep it
> intact. Anyway, I need to break it up into 2 statements inside wndproc.
> What is the correct statement in this case?

Correct? Dunno, depends on your code. Anyhow, there are (at least) three
separate issues here:
1. You can not assign a raw pointer to an auto_ptr, you have to use reset().
2. The various case labels in a switch statement don't have a separate
scope, they all share access to their common scope. You must not skip a
call to a constructor (initialisation), which is what the compiler is
complaining about. Use a separate code block instead:
case ...:
{
....
}
break;
case ...:
3. All variables inside a function are local. If your windowproc is called,
it will each time create a new set of locals and on exit destroy them. This
might not be what you want.

Points 1 & 2 are completely independent of your current programming
problems. If you had actually taken the time to create a minimal test case,
as is customary on the Usenet, you could have asked more precise questions
and gotten more precise answers or even solved the problem yourself.

Uli

--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932