Re: A few basic questions about constructor & exception (in Managed C++ environment)
- From: Tamas Demjen <tdemjen@xxxxxxxxx>
- Date: Mon, 08 Aug 2005 12:11:27 -0700
First, you should really use delete [] instead of delete in this case. Second, I wouldn't call the destructor directly. I recommend that you introduce a Cleanup() function that you call from both the d'tor and the c'tor.
But if you really want to know my opinion, I simply wouldn't use native pointers at all. They're unsafe, from both the exception safety point of view, and from the programmer mistakes point of view.
I would simply use std::vector in most cases. That's a reasonable container. IF you know the number of items, you can use reserve to pre-allocate memory for it. Instead of TYPE_1_T *, I would use vector<TYPE_1_T>.
If that doesn't sound good, my second choice would be boost::scoped_array, which is a smart pointer similar to auto_ptr, but for arrays. It calls delete [] instead of delete to deallocate. The only time I like scoped_array better than vector is when I need to allocate uninitialized memory for extreme perforamance reasons. It doesn't happen in typical application code, but in low-level library code, such as image processing.
Both solutions are perfectly exception safe and you can't make a mistake with them, and you don't have to worry about throwing from the construtor.
If I really had to use native unsafe pointer, I would consider calling malloc and free directly, which don't throw.
Otherwise your code is working too, it's just cumbersome, difficult to write, maintain and read, and it's easy to make a mistake there. For example, if you later need to add a var6, you have to modify the code at many different places, and if you just miss one of them, you'll get into trouble. A good design is that requires the minimum amount of editing in order to introduce an additional variable.
This is just an advice, don't feel bad about it, your solution basically works (after changing delete to delete []).
Tom
Lloyd Dupont wrote:
class MyClass
{
TYPE_1_T *var1, *var2, *var, *var4, *var5;
inline void SetNull()
{
var1 = NULL;
var2 = NULL;
var3 = NULL;
var4 = NULL;
var5 = NULL;
}
public:
MyClass()
{
// NULL them first, so I know what to delete
SetNull();
try
{
// some (memory) error throwing init code
var1 = new TYPE_1_T[SomeNumber];
// ... alloc/new other variable
}
catch
{
// here I delete all my allocated var, what do you think?
~MyClass();
throw NotEnoughMemory ;
}
}
~MyClass()
{
if(var1)
delete var1;
if(var2)
delete var2;
if(var3)
delete var3;
if(var4)
delete var4;
if(var5)
delete var5;
// just in case, I'm not really confident about C++
// could I have ~MyClass() private?
SetNull();
}
}
.
- Follow-Ups:
- References:
- A few basic questions about constructor & exception (in Managed C++ environment)
- From: Lloyd Dupont
- Re: A few basic questions about constructor & exception (in Managed C++ environment)
- From: Tamas Demjen
- Re: A few basic questions about constructor & exception (in Managed C++ environment)
- From: Lloyd Dupont
- Re: A few basic questions about constructor & exception (in Managed C++ environment)
- From: Nemanja Trifunovic
- Re: A few basic questions about constructor & exception (in Managed C++ environment)
- From: Lloyd Dupont
- A few basic questions about constructor & exception (in Managed C++ environment)
- Prev by Date: Re: beginner question about auto_ptr
- Next by Date: VC7.1 new project question
- Previous by thread: Re: A few basic questions about constructor & exception (in Managed C++ environment)
- Next by thread: Re: A few basic questions about constructor & exception (in Managed C++ environment)
- Index(es):
Relevant Pages
|