
-----------------------------------
Clayton
Mon May 29, 2006 11:39 am

array of classes?
-----------------------------------
is it possible to create an array of classes? ex.

class Monster
end Monster

var enemy:array 1..100 of ^Monster

if so, how would you call such a class in the array?

-----------------------------------
wtd
Mon May 29, 2006 12:24 pm


-----------------------------------
Well, if that works, then each element in the array is a pointer to a Monster object.

If you just had:

var m : ^Monster

How would you create a new Monster?

-----------------------------------
MysticVegeta
Mon May 29, 2006 12:59 pm

Re: array of classes?
-----------------------------------
is it possible to create an array of classes? ex.

class Monster
end Monster

var enemy:array 1..100 of ^Monster

if so, how would you call such a class in the array?
Its not really an array of classes but rather a pointer to the monster.

-----------------------------------
TheOneTrueGod
Mon May 29, 2006 1:45 pm


-----------------------------------
What you have is correct, though you might as well use flexible arrays as well.  If you were going to call it, you'd simply need to do something like:


class Monster
   procedure DrawMe(x,y : int)
         drawfilloval(x,y,5,5,black)
   end DrawMe
end Monster
var m : array 1..2 of ^Monster
for i : 1..upper(m)
   new Monster, m(i)
end for
%Now, there are two ways to reference 'DrawMe'
m(1) -> DrawMe(100,100)
Monster(m(2)).DrawMe(200,100)


And thats all.

-----------------------------------
Cervantes
Mon May 29, 2006 4:26 pm


-----------------------------------
Not so very long ago we had TheOneTrueGod make the same mistake: pointers to that class, less syllabic intensively referred to as objects.

Only in a programming language like Ruby can you have an array of classes, because in Ruby classes are objects.
Well, they aren't truly objects. In the actual source for Ruby, they are made to mimick objects. But to the Ruby programmer, they act like objects. New classes can be created dynamically by calling Class.new

-----------------------------------
wtd
Mon May 29, 2006 5:10 pm


-----------------------------------
Not so very long ago we had TheOneTrueGod make the same mistake: pointers to that class, less syllabic intensively referred to as objects.

Only in a programming language like Ruby can you have an array of classes, because in Ruby classes are objects.
Well, they aren't truly objects. In the actual source for Ruby, they are made to mimick objects. But to the Ruby programmer, they act like objects. New classes can be created dynamically by calling Class.new

No...

You have an array of pointers to instances of a given class (or some subclass).

Instances of a class are also known as objects.

And in Ruby classes are indeed objects; and not simply things which resemble objects.  This is difficult to reconcile until one realizes that Ruby's internals are prototype-based, while it externally represents this as "class-based" behavior.

-----------------------------------
Cervantes
Mon May 29, 2006 5:46 pm


-----------------------------------
You have an array of pointers to instances of a given class (or some subclass).
Oops. :oops:
Though the point still has some validity. There are two different syntaxes for this:
var p : ^MyClass
var p : pointer to MyClass
Since we all love shorthand, the ^MyClass notation is more common. However, when looking at this, it is easy to miss the carat (^) and thing that you're variable actually has a datatype MyClass, when in fact the real data type is "pointer to MyClass", which represents the MyClass object, or the instance of MyClass.

And in Ruby classes are indeed objects; and not simply things which resemble objects.
But! but! Why said so!

wtd, I recommend you read Why's article, "
I hate to break it to you, but a class isn't really an object. From Ruby's source code:


 struct RObject {
   struct RBasic basic;
   struct st_table *iv_tbl;
 };

 struct RClass {
   struct RBasic basic;
   struct st_table *iv_tbl;
   struct st_table *m_tbl;
   VALUE super;
 };


Look! A class has an m_tbl (a symbol table for storing methods) and a superclass (pointer to a superclass).

But let me reassure you. To a Ruby programmer, a class is an object. Because it meets the two big criteria: you can store instance variables in a class and it is descended from the Object class. That's it.


-----------------------------------
Clayton
Mon May 29, 2006 9:05 pm


-----------------------------------
thnx for all of the help guys, im trying to get into more OOP and was unsure if you could create an array of pointers to a class (not an array of classes) thanks for clearing that up :D

-----------------------------------
wtd
Mon May 29, 2006 9:10 pm


-----------------------------------
And in Ruby classes are indeed objects; and not simply things which resemble objects.
But! but! Why said so!

Quiet you!
