Quote:Original post by CoreMeltdown
I'm really sorry... it wasn't your solution.
But anyway... your solution has local pointers...
Maybe I wasn't clear, I'll try to explain better, writing a dummy example in C:
*** Source Snippet Removed ***
You still havn't been clear if you want C or C++. Here is the same thing in C++, not using local pointers this time:
struct MyStruct{ boost::shared_array<MyType> p1; boost::shared_array<MyType> p2; boost::shared_array<MyType> p3;};std::auto_ptr<MyStruct> MyFunction(size_t dummySize){ std::auto_ptr<MyStruct> ms(new MyStruct); ms->p1(new MyType[dummySize]); ms->p2(new MyType[dummySize]); ms->p3(new MyType[dummySize]); // if(!CheckOrUse(p3)) return std::auto_ptr<MyStruct>(); return ms;}
You may notice I've not checked the pointers for NULL (as you do in the code I'm working off). If they somehow fail to allocate (or a constructor fails), then it will throw an error (unless you are specificly writing with a non-throwing new).
If you're still doing the CheckOrUse function, I've left in an example use. Alternitivly you may want to catch the std::bad_alloc error and return a NULL auto pointer to match the behaviour of your old system if new fails. Alternitivly, you could have CheckOrUse throw an error if it fails (which matches the behaviour if new failed and could be nicer than returning NULL pointers).
Of course, you could go even more C++'ish, you could do it like the following. I've also left a sample CheckOrUse call in there. In this case, it must throw (being in a constructor). IMO this is the nicest version of the code.
class MyClass{ boost::shared_array<MyType> p1; boost::shared_array<MyType> p2; boost::shared_array<MyType> p3;public: MyClass(size_t dummySize) { ms->p1(new MyType[dummySize]); ms->p2(new MyType[dummySize]); ms->p3(new MyType[dummySize]); // if(!CheckOrUse(p3)) throw SomeErrorClass(); }};