Objective-C; typedef objc_object as substitute for id without pointer; -


in objective-c id typedef:

typedef struct objc_object {      class isa; } *id; 

so can declare (and initialize) variable e.g. this:

// using id id po_one = @"one"; 

compiles fine.

since name types in own scheme , since dislike implied pointer in id typedef want add own typedef (with o object) this:

typedef struct objc_object o; 

so variable declaration (with initialization) like:

// using o o * po_two = @"two"; 

this compiles warning:

initialization incompatible pointer type

as far understand typedefs thought latter should equivalent to:

// using struct objc_object struct objc_object * po_three = @"three"; 

which again compiles fine.

it's astonishing that:

po_two = po_one; po_two = po_three; 

both compile without warnings.

so tried:

typedef struct objc_object * po; 

to see whether works without warning if include pointer (being thing want avoid - test reasons) see whether typedef outside of structure definition works differently typedef in structure definition (which definition of id in case).

// using po po po_four = @"four"; 

that works fine.

if use:

#define o struct objc_object 

the construct using o again compiles without warning.

but prefer use typedefs instead of defines whenever possible.

i'm puzzled. misunderstanding typedefs?

i'm not expert of compilers, real expert can give better answer. in case, based on knowledge, when compiler type checking, bases check on "parse tree", generates sort of table possible data type structures, , check if 2 definitions point same row in table. works fetching symbols , type definitions table , comparing them. "struct objc_object" 1 of these data structures. then, when id defined, generates entry: "id --> struct objc_object *". po_three definition leads same reference: "struct objc_object *" (infact "=" has lowest precedence). same happens po_four, because po leads definition same reference, again (po --> struct objc_object *). when use definition #define, preprocessing you're still referencing "struct objc_object *". when use

o * po_two = @"two"
you're in fact trying match 1 type "id" (@"two"), "struct objc_object *" , 1 pointer "struct objc_object" if logically same don't lead same reference in table. reason because "o" type has been defined yet, po_two stored in symbol table pointer "o" , "o --> struct objc_object". , that's reason of warning: po_two pointer different @"two".

i have curiosity: have tried plot symbol table using "nm"?


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -