1

Please consider these 2 code statements:

id class1 = [[MyClass alloc]init]; //First 
MyClass * class2 = [[MyClass alloc]init]; //Second

class1 and class2 are pointers to type id right? In a header file of objc, I saw that Class is just typedef of a struct pointer, and id is typedef of a struct pointer as well. With those types being declared as pointers to struct, why do I need to include a * in the second statement of my code?

user007
  • 21
  • 2

2 Answers2

1

Because id a pointer to struct already:

typedef struct objc_object {
    Class isa;
} *id;

Where as MyClass isn't a pointer.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • But classes are of type `Class`, `Class` is pointer to `struct`. – user007 Oct 26 '13 at 17:22
  • 1
    @user007 No they're not; `Class` describes the class from which instances are created. `MyClass *` points to an instance of a class. – trojanfoe Oct 26 '13 at 17:23
  • So you mean that classes are of type `struct`? So how do classes fit in the argument of `objc_msgSend` which requires a type `id`? – user007 Oct 26 '13 at 17:29
  • @user007 Because if `id` is `struct *` then `objc_msgSend()` simply accepts a pointer to the `struct` (which is how you'd normally pass `structs` to functions). – trojanfoe Oct 26 '13 at 17:40
  • how about for classes. How can you pass pointer to structs if they aren't pointer to struct? – user007 Oct 26 '13 at 18:19
  • @trojanfoe: `Class` is indeed a type that can hold pointers to class objects – newacct Oct 28 '13 at 22:17
  • @user007: A pointer to a class object can be held in a variable of type `Class`, just like a pointer to any object can be held in a variable of type `id`. How is that relevant? – newacct Oct 28 '13 at 22:26
0
  • id is a type. It can hold a pointer to any object.
  • Class is a type. It can hold a pointer to any class object. (A class object is the object representing a class. i.e. the object that gets messaged when you call a class (+) method.)
  • MyClass * is a type. It can hold a pointer to any object that is an instance of MyClass

class1 and class2 are pointers to objects.

You seem to have some big confusion between class objects and instances of classes. A MyClass * is a pointer to a MyClass instance, i.e. an object whose class is MyClass. That is completely different from the class object representing the MyClass class itself. The MyClass class object is not an instance of MyClass. Class objects are a more advanced topic that you should probably not think about now.

newacct
  • 119,665
  • 29
  • 163
  • 224
  • I think this is a confusing topic in general. There's also the bit about knowing what's runtime and compile-time, which seems related. – nielsbot Oct 28 '13 at 22:37
  • @newact: If you say that `MyClass *` is a type then it would be like, `aStruct * *` right? If I am right, could you please explain what it means, cause I think that this is the one last thing that confuses me. Thanks. – user007 Oct 29 '13 at 21:22
  • @user007: `MyClass` is not a legal type in Objective-C, but if it were legal, it would be a struct type, just like class types in C++. So `MyClass *` is the type of a pointer to some struct. I think your confusion comes from the fact that there's a type named `Class` that is an object pointer type, and you somehow think that anywhere you see the name of a class (e.g. `MyClass`), that you can replace it with `Class`. But it's nothing like that. "`Class`" is simply the name of a special type in Objective-C. It has nothing to do with the type of instances of some class (e.g. `MyClass`). – newacct Oct 29 '13 at 23:14