Does Obj-C Allow for Recursive Pointers?

Discussion in 'Mac Programming' started by BadWolf13, Oct 19, 2010.

  1. BadWolf13 macrumors 6502

    Dec 17, 2009
    In most programming languages I've worked with, you can't have an class that contains and instance of it's own class. Naturally, you'd get compiler errors. But as I'm reading through Obj-C literature, I'm wondering if the use of pointers makes it possible. For instance;

    @interface myObject : NSObject {
    	myObject *containedObject;
    Or, if I did it using dynamic typing;

    @interface myObject : NSObject {
    	id containedObject;
    Could I then have containedObject point to another object of the same class, or would this produce errors during runtime?
  2. iSee macrumors 68040


    Oct 25, 2004
    There's no problem with having an instance of a class point to an instance of the same class.
  3. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    Being able to have an instance of the same type as the containing class is fairly normal/mundane. What languages disallow this? (I'm sure there are some, just don't know which ones) Without this pattern you wouldn't be able to do fairly ordinary things like build a linked list.

  4. iSee macrumors 68040


    Oct 25, 2004
    What would have to be disallowed is where an instance of a class must contain an instance of that same class (directly or indirectly). That would create a situation where one instance of the class leads to an infinite chain of contained instances. A must contain an instance of A which must contain an instance of A which...

    e.g., (C++)
    class A
      A a;
    With pointers/references, however, there is no contained instance, so no infinite chain. (Well, you could still attempt to create an infinite chain at runtime).
  5. BadWolf13 thread starter macrumors 6502

    Dec 17, 2009
    Thank you iSee. C++ is my background. I was pretty sure I wasn't allowed to do that in that language.

    Wait, does that mean my first example won't work? Or will it work because it's a pointer to another instance of it's own class, instead of a contained instance of it's class?
  6. admanimal macrumors 68040

    Apr 22, 2005
    Your example is OK, because the pointer doesn't actually point to a concrete instance of myObject until you explicitly allocate it.
  7. BadWolf13 thread starter macrumors 6502

    Dec 17, 2009
    As The King used to say, "thank you very much."
  8. SidBala macrumors 6502a

    Jun 27, 2010
    Having pointers pointing to the same class type or even the same instance as the class is allowed in C++. So your example will work.
  9. Sydde macrumors 68020


    Aug 17, 2009
    Note that the declaration
    NSObject anObject;
    will be rejected by the Objective-C compiler because you are not allowed to create a "statically allocated instance" of an object: you can only work with pointers to objects. Because of this, no object can ever physically contain another object, only a pointer to it. "Containing" an instance is an abstract behavior, as described in the section on Object Ownership and Disposal - you can think of an object as "contained" within another object if the containing object's -dealloc method releases it, and even then, the contained object could possibly be taken and retained by some other object. If the statically allocated declaration above were allowed as an ivar, a pointer to it could be passed using the form "&anObject", whereafter the containing object would have to continue to exist for that pointer to be valid. Prohibiting static allocation makes a lot of sense.

    Consider that your object can have an ivar of type id, which can be any class of object.
  10. zeppenwolf macrumors regular


    Nov 17, 2009
    From _The C Programming Language_, K&R, Section 6.5, "Self-Referential Structures":


Share This Page