struct
{
int integer;
float real;
}
first_structure;
So we can refer to a member of the first_structure by writing
first_structure.integer = 7
If I write:
struct two_numbers
{
int integer;
float real;
}
first_structure;
then I can use the tag *two_numbers* to create a second structure like this:
struct two_numbers second_structure;
I also understand that typedef can be used to create synonyms.
But I am unable to understand the code below (from the page http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocObjectsClasses.html):
typedef struct objc_class *Class;
typedef struct objc_object {
Class isa;
} *id;
Every object thus has an isa variable that tells it of what class it is an instance.
HOW can it tell???? Please guide me through the meaning of this code.
Thank you.
That’s a shortcut to:
The
idtype is a pointer to anobjc_objectstructure.So when using an
idtype, you will use the->operator instead of., since it’s a pointer.Note that the
Classtype is also a pointer to a structure.That’s called an opaque type.
It basically means the compiler will be able to calculate the size, since we have only a pointer, but it won’t know the implementation details.
That kind of pattern is used to hide the actual implementation of a structure.
The type is defined in a header file, but the implementation is only defined in a source file.
For instance, in a header file:
That’s valid, and it defines a type to a structure pointer, even if that structure is not known actually.
Then in a source file:
Here’s the real implementation. Users will be able to create
Atyped objects, but won’t be able to access the members, since they are known only in your source file.Users will pass you
Atyped objects, but you’ll be cast them to astruct A, so you can access it members.IE: