April 18, 2024, 05:23:36 PM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Object Questions

Started by definitelyokay, December 01, 2009, 03:35:31 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

definitelyokay

Still grasping all this good oop stuff! So far, I'm loving it. (In the process of writing some classes for a project).

Ok, so here's what I understand:

You can create an object indirectly using a pointer and the New() function. When you're done with it, you can just delete it. That makes sense.

Question 1: Can I use Delete on object created directly? Here's what I mean:

Dim myObject As SomeObject
Delete myObject

Question 2: Also, can you pass objects into subs without doing it the pointer style? Example:

Sub doSomething(instance As SomeObject)

Also, if I have another object inside of a class that the class uses, is it automatically removed as well when the object is destroyed since it is local to the class?

Question 3: Does SetType on an object set the type of the local object (if you're in a sub), or is it only for global use? Can I use it on an array of pointers?

Question 4: And, one more thing. What's the best way to store a list of objects? If I understand, I can put objects in a linked list, but I have to dereference them to use them, whereas if I put them in an array, I can use them directly?

Any help would be much appreciated. Sorry about all the questions!

sapero

December 01, 2009, 05:58:43 PM #1 Last Edit: December 01, 2009, 06:30:40 PM by sapero
1. No, because the object is allocated from thread stack memory pool, which is automatically released when the current subroutine returns. If you use NEW to allocate a object, you are receiving a memory block from process heap, the returned pointer is valid until you call delete, even if you quit current subroutine. Delete operator before actualy deleting, checks if the given pointer to delete is from one type of memory pools (there is stack, heap, local, global, virtual, physical, mapped, ... memory). Each type of memory has its own allocators, and you cannot allocate memory of one memory type (heap) and free it as another type (virtual). If the pointer you are deleting is from thread stack instead from 'heap', your program may be terminated.

Your example uses stack memory, this type of memory "always" exists - is allocated by the operating system when a thread begins execution, and there is no function to allocate and free stack memory (there are such: _alloca, VirtualAlloc, VirtualFree, but you must understand how it is working to use them). Actually to allocate a memory block on the stack, only a processor register (ESP - extended stack pointer) needs to be modified, but this technique is not directly supported in Ebasic.

2. Yes, but remember that (in Ebasic) it will be passed by reference, not by value (not a copy). Example:
class vector
int x,y,z
endclass

sub mysub(vector c)
c.y = 9
endsub


3. SetType on an object pointer - SetType can be used on pointers only, the scope of SetType is same as lifetime of the associated variable. If you have a global pointer and set its type to POINT somewhere on the top of your source code, the type will be known anywhere after SetType. If you call SetType again on the same (global) variable, the type will change from the point where you called SetType, to the end of source code. If your pointer is local - defined in a subroutine, the type of pointer set by SetType will be known only in current subroutine.
If you dereference a pointer using *<NEWTYPE>variable, the type of variable will change to NEWTYPE, in the same way as with SetType. // not in current Ebasic

4. Ebasic has "Each" keyword - enumerates nodes from linked list and automatically sets pointer type to whatever you want:
pointer mylist = your linked list
pointer object
FOR object = EACH mylist AS vector /* AS YOURTYPE */
PRINT *object.x, *object.y, *object.z /* use * or # */
*object.rotate(0.1, 0.2, 0.3) /* example method */
NEXT

definitelyokay

Sapero, that is an excellent explanation! :o

It took me 4 or 5 reads to understand (due to my newness on the subject), but it all makes perfect sense to me now. Thanks for answering those questions, as I've been wondering about them for some time now. ;D

The more OOP I do, the more I wished I had learned about it long ago... It's rather addictive. You get this feeling of "Hey, this is so cool! I can turn this into a class, as well as this, and this..."

Of course writing one class causes me to write another one to go with that one, and so forth and so on. It seems you have to be careful not to go overboard with this stuff. :P

As a little side note, is it good practice to make a class for something you'll only need one instance of? For example, if you have a FileManager class that you have an instance of called FileHelper (which you use to do your file management), and that's the only instance you have, is it worth it?

I was just wondering, because I see lots of code at different places that only implements a single instance of an object. It's like they use OOP just to organize their code and make it look pretty. Is that good practice?