From: n00m on
What's wrong with this class code?

class Roo {
public:
Roo(int sz) {ar = new int[sz];}
~Roo() {delete ar;}
private:
int* ar;
};
From: Ulrich Eckhardt on
n00m wrote:
> What's wrong with this class code?
>
> class Roo {
> public:
> Roo(int sz) {ar = new int[sz];}
> ~Roo() {delete ar;}
> private:
> int* ar;
> };

We're missing the address where we should send the answer to that homework
question.

Uli

From: Richard Heathfield on
In
<12727004-6ce5-46d7-822e-eb2080338f4d(a)j24g2000yqa.googlegroups.com>,
n00m wrote:

> What's wrong with this class code?
>
> class Roo {
> public:
> Roo(int sz) {ar = new int[sz];}
> ~Roo() {delete ar;}
> private:
> int* ar;
> };

Why doesn't this code print "yes"?

#include <stdio.h>

int main(void)
{
int w = 42;
int x = 6;
int y = w * x;
int z = y - x;
printf("%s\n", z == w ? "yes" : "no");
return 0;
}

If you can answer that, you're well on the way to understanding what's
wrong with your class code.

Yes, this is a rather cryptic response. But don't spend too long
trying to decode it. If you're bright enough to get my rather
esoteric hint, you're certainly bright enough to look up dynamic
array allocation in your C++ book.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
From: n00m on
Because it's False:

>>> w = 42
>>> x = 6
>>> y = w * x
>>> z = y - x
>>>
>>> z == w
False
>>>


> ... with your class code ...

"[]" missed there.
I'm still on a curve of accustoming myself to syntax etc.
Logically it's obvious that "delete" was supposed to free
*all* memory allocated for array "arr".
From: n00m on
P.S.
I would like not to see here words like "homework", "RTFM" etc.
If you don't know e.g. Python, even 5 books on it will not help
you in a minute or so to find/to guess the right answer for this
(no matter how smart you are (or are not)):


>>> a = [1, 2, 3]
>>> b = a
>>> b[0] = 100
>>> a[0] + b[0]

?
>>>