Home

barcaroller wrote:
> I have an object that throws an exception when the constructor fails. I
> could construct it inside a try/catch block, but then the object is no
> longer visible outside the block.
>
> try
> {
> classA objA;
> }
> catch (...)
> {
> // handle the exception
> }
>
> objA.doStuff() // Wrong! Out of scope!
>
>
>
> I do not want to move the rest of the program inside the try/catch block.
> How do I work around this?



std::auto_ptr<classA> pobjA;

try
{
pobjA = std::auto_ptr<classA>( new ClassA );
}
catch (...)
{
// handle
}

pobjA->DoStuff();


If it must me on the stack, you can use an alloca (non-standard) see
here: http://groups.google.com/group/comp.lang.c++/msg/64bf343b7f5 b76c1?.

previous
next

Re: how to add a toolbar to a Frame using wxpython
Re: How we recognize newbies as newbies
Re: Newbie packages Q
Re: File access
AttributeError: 'module' object has no attribute 'HTTPSHandler'
Nasze Dzieci
Fundacja Iskierka
Mimo Wszystko
Pajacyk
Fundacja Sloneczko