Re: NSPoint and automatic constructor
Re: NSPoint and automatic constructor
- Subject: Re: NSPoint and automatic constructor
- From: Robert Goldsmith <email@hidden>
- Date: Mon, 24 May 2004 04:11:37 +0100
>
Implicit/automatic constructor is not a defined term AFAIK, just what
>
we call a currently non-existing feature.
You have to be careful here because, although what you say is true for
C, it is not true for C++. C++ DOES have implicit constructors which
simply clear all instance variables to 0 if you don't specify a
constructor yourself. C, obviously, cannot have constructors because
constructors deal with the initialisation of objects, which C doesn't
have. Structs should be thought of as complex primitives rather than
like objects. They follow the same rules as ints and floats and chars.
You would not say you construct an int.
>
In Java there is only heap storage, and you allocate from it using
>
'new' and thus get a pointer to somewhere in the heap.
Really? So local scope variables still sit on the global heap? hmm. I
feel sorry for the Java garbage collector.
>
C/C++ also has automatic storage, generally this is just the stack. So
>
if you do not write 'new', the object is placed on the stack (and will
>
be destroyed when you go out of scope).
If you have got your head round autorelease pools, it's a similar idea
in principle. Just think of the compiler creating it's own 'autorelease
pool' for all the stuff created implicitly in a method and then just
clearing the pool when the method exits. Of course, a primitive type
returned from a method is simply copied and returned on the stack but
you cannot pass back a pointer to a struct or any of the local
variables because it will point into a trashed memory block once the
method returns. Therefore, you have to create the item explicitly with
new or malloc() (c++ or c way respectively) then remember to tidy up
when you are done (with delete or mfree()) or create the item before
the method and pass it in my-reference (pass a pointer to it) and
modify that in the method.
I strongly feel that one of the biggest problems with teaching Java -
esp. for students then wishing to move on to other languages - is that
all of this is hidden. I consider knowing at least the basics of memory
management and variables 'work' is important :)
Robert
---
GnuPG public key:
http://www.Far-Blue.co.uk
[demime 0.98b removed an attachment of type application/pgp-signature which had a name of PGP.sig]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.