On 16 avr, 23:43, mpho <tjab...@[EMAIL PROTECTED]
> wrote:
> class X {
> int sz;
> T *ptr;
>
> public:
> X(int s, T *p) : sz(s), ptr(new T[s])
> {
> assert(ptr != NULL);
> for (int i = 0, i < sz; i++)
> ptr[i] = p[i];
ptr[i] = p[i] can throw.
If it does, you need to call delete[] on ptr.
> ~X() { delete ptr; ptr = NULL; }
You should use delete[], not delete. Calling delete on something
allocated with new[] is undefined behaviour.
ptr = NULL is also totally useless in a destructor.
By the way, the automatically defined operator= and copy constructor
will mess everything up. Either disable them, or write them to do deep-
copying.
> class Y {
> X xobj
>
> public:
> Y(int s, T *p) : xobj(s, p) { } //OK?
> //other members
> ~Y() { } //OK?
> };
That's OK, yes.
> ar[10] = {ten values};
That doesn't seem to be valid C++ syntax.
> Y yobj(10, ar); //problem here(?)
None at that line.
> What's the problem above?
How is a std::bad_alloc being raised a problem?
It could just means you're out of memory.
--
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|