Discussion:
Initializing an object with auto pointers
(too old to reply)
Jack
2010-04-12 10:45:25 UTC
Permalink
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
Jack
2010-04-12 10:48:06 UTC
Permalink
Post by Jack
std::auto_ptr<BFThread> thread1;
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
Alex Blekhman
2010-04-12 11:03:23 UTC
Permalink
Post by Jack
std::auto_ptr<BFThread> thread1;
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
Jack
2010-04-12 11:40:08 UTC
Permalink
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
Ulrich Eckhardt
2010-04-12 12:06:55 UTC
Permalink
Post by Jack
Does that mean objects declared with std::auto_ptr only live once and
simply be gone and can't be reused?
A std::auto_ptr instance is an object like any other object, too, there are
no exceptions to it.

That said, auto_ptrs are used to ensure _exclusive_ ownership (i.e.
ownership by exactly one) for the pointee (the one it points to). Whether
that fits your design is up to you to decide.

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

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Alex Blekhman
2010-04-12 14:02:05 UTC
Permalink
Post by Jack
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.
In addition to Ulrich's reply. Yes and no. You can always give pointer
ownership with auto_ptr::release call. However, the main advantage of
auto_ptr is to call `delete' automatically on the stored pointer when
auto_ptr instance goes out of scope. That's why it is called auto_ptr
("automatic pointer") to begin with.

Sometimes this is exactly what you want:

void foo()
{
std::auto_ptr<MyClass> ptrMy(new MyClass);

// use ptrMy as regular poiner
ptrMy->Bar();
...

// No need to call `delete', std::auto_ptr will take care of
// the pointer upon regular exit or exit via exception.
}

HTH
Alex

Ulrich Eckhardt
2010-04-12 11:49:41 UTC
Permalink
Post by Jack
std::auto_ptr<BFThread> thread1;
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
Loading...