Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zippyfly

macrumors regular
Original poster
Mar 22, 2008
141
0
Hello. Need some help from the ObjC pros.

id generalData;
SpecificA *spec1 = [SpecificA new];
SpecificB *spec2 = [SpecificB new];

generalData = spec1;
...
generalData = spec2;
...

QUESTION: Why is it not

id *generalData;

missing the dereferencing operator *???

Aren't all objects in ObjC really just pointers to struts? And therefore, the id "superclass" type should have generalData declared as a pointer, no?

Thanks,
Quite Confused.
 

HiRez

macrumors 603
Jan 6, 2004
6,265
2,630
Western US
QUESTION: Why is it not

id *generalData;

missing the dereferencing operator *???

Aren't all objects in ObjC really just pointers to struts? And therefore, the id "superclass" type should have generalData declared as a pointer, no?

Thanks,
Quite Confused.
id already is implicitly a pointer by definition, so you don't need to add the pointer specifier. I personally think they shouldn't have built it in (hidden it), and instead forced you to type id *myThing, as I think it would be more clear. But just read it as "id *" when you see it.
 

zippyfly

macrumors regular
Original poster
Mar 22, 2008
141
0
Thanks! Phew. You have no idea how much you helped clear up. It is really confusing for a beginner.

You are right, it should have been implemented to be declared specifically. After all, a lot of the syntax is supposed to force us to be clear about things and for the compiler to help check stuff at compile time.

I wonder if I can still use * without a compiler error? (Will try it later with gcc).

Actually, do you (or anyone else) have any general guideline about when to use * and when not to? (I still have not grokked a "golden rule" yet about this).

I know about using * to dereference, etc. a-la standard C; I am just sometimes confused why we use * and sometimes not, when dealing with objects.

I figured that anything inside the [brackets] is ObjC helping to "pre-process" the OOP stuff, so we don't use * inside the brackets, but outside of them, it sometimes gets confusing since there seem to exceptions to rules (as in the abovementioned original problem I had).

TIA!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.