Computer Science Canada

array of classes?

Author:  Clayton [ Mon May 29, 2006 11:39 am ]
Post subject:  array of classes?

is it possible to create an array of classes? ex.
Turing:

class Monster
end Monster

var enemy:array 1..100 of ^Monster

if so, how would you call such a class in the array?

Author:  wtd [ Mon May 29, 2006 12:24 pm ]
Post subject: 

Well, if that works, then each element in the array is a pointer to a Monster object.

If you just had:

code:
var m : ^Monster


How would you create a new Monster?

Author:  MysticVegeta [ Mon May 29, 2006 12:59 pm ]
Post subject:  Re: array of classes?

SuperFreak82 wrote:
is it possible to create an array of classes? ex.
Turing:

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.

Author:  TheOneTrueGod [ Mon May 29, 2006 1:45 pm ]
Post subject: 

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:

code:

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.

Author:  Cervantes [ Mon May 29, 2006 4:26 pm ]
Post subject: 

Not so very long ago we had TheOneTrueGod make the same mistake: http://www.compsci.ca/v2/viewtopic.php?p=115832#115832

You don't have an array of classes. You have an array of 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

Author:  wtd [ Mon May 29, 2006 5:10 pm ]
Post subject: 

Cervantes wrote:
Not so very long ago we had TheOneTrueGod make the same mistake: http://www.compsci.ca/v2/viewtopic.php?p=115832#115832

You don't have an array of classes. You have an array of 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.

Author:  Cervantes [ Mon May 29, 2006 5:46 pm ]
Post subject: 

wtd wrote:
You have an array of pointers to instances of a given class (or some subclass).

Oops. Embarassed
Though the point still has some validity. There are two different syntaxes for this:
code:
var p : ^MyClass

code:
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.

wtd wrote:
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, "Seeing Metaclasses Clearly", specifically the section entitled "Classes Are Objects". Even more specifically, this:

why wrote:

I hate to break it to you, but a class isn't really an object. From Ruby's source code:

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.

Author:  Clayton [ Mon May 29, 2006 9:05 pm ]
Post subject: 

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 Very Happy

Author:  wtd [ Mon May 29, 2006 9:10 pm ]
Post subject: 

Cervantes wrote:
wtd wrote:
And in Ruby classes are indeed objects; and not simply things which resemble objects.

But! but! Why said so!


Quiet you!


: