discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Programming it's a play


From: Pascal J . Bourguignon
Subject: Re: Programming it's a play
Date: Sun, 21 Sep 2003 02:00:26 +0200

Nicolas Roard writes:
> > Please correct me if I am wrong since it is only since today that I feel I 
> > realy understand the difference between a Class, a Class object, an Object 
> > and an Instance.
> 
> Well, imho you shouldn't speak about a "class object" because it's a
> bit misleading -- it's not really an object. And an object IS an
> instance, it's the definition. It's an instance of a class of
> objects.

Classes are objects  that most often are created  at compilation time,
but not necessarily.   You can easily create a  class object and class
and instance methods at runtime,  then instanciate this class and send
messages to this object.

That is, you  can easily do this in Objective-C or  in Smalltalk or in
Lisp, because they  are dynamical languages, while it's  so hard as to
be impossible in C++, but we're not discussing C++.

(I  must admit that  there is  a little  problem in  Objective-C, that
there is  no standard API  to the objc  runtime, so doing it  would be
compiler dependant).


To see that there is no difference between a class and an object, just try:

#include <objc/Object.h>
#include <stdio.h>
#include <stdlib.h>

   static void print_name(id thing)
   {
       printf("name of thing at %08p is: %s\n",thing,[thing name]);
   }

   @interface NamedObj:Object
       {
          char* myName;
       }
       -(void)setName:(const char*)newName;
       -(const char*)name;
   @end

   @implementation NamedObj
       -(void)setName:(const char*)newName
       { 
           myName=(char*)malloc(strlen(newName)+1);
           strcpy(myName,newName);
       }
       -(const char*)name
       {
          return(myName);
       }
   @end

   int main(int argc,char** argv)  
   {
       id    anObject=[[Object alloc]init];
       Class aClass=[anObject class];
       id    anotherObject=[[NamedObj alloc]init];
       Class anotherClass=[anotherObject class];
       [anotherObject setName:argv[1]?argv[1]:"John Doe"];
       print_name(anObject);
       print_name(aClass);
       print_name(anotherObject);
       print_name(anotherClass);
       return(0);
   }


[pascal@thalassa tests]$ gcc -o objc-class-objc objc-class-objc.m -lobjc 
-lpthread
[pascal@thalassa tests]$ ./objc-class-objc toto
name of thing at 0x8058790 is: Object
name of thing at 0x8056520 is: Object
name of thing at 0x80587d0 is: toto
name of thing at 0x8055f40 is: NamedObj


So, you see  that you can have an class object  or an instance object,
and not knowing what it is, send it a message and get work done.



Now,  in Objective-C, there  IS a  difference between  metaclasses and
other objects.  In Smalltalk and in Lisp, there is none. 

-- 
__Pascal_Bourguignon__
http://www.informatimago.com/
Do not adjust your mind, there is a fault in reality.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]