Does Obj-C Allow for Recursive Pointers?

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

  1. macrumors 6502

    Joined:
    Dec 17, 2009
    Messages:
    267
    #1
    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;

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

    Code:
    @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. macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    Messages:
    3,325
    #2
    There's no problem with having an instance of a class point to an instance of the same class.
     
  3. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Messages:
    3,172
    Location:
    Dallas, TX
    #3
    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.

    -Lee
     
  4. macrumors 68040

    iSee

    Joined:
    Oct 25, 2004
    Messages:
    3,325
    #4
    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++)
    Code:
    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. macrumors 6502

    Joined:
    Dec 17, 2009
    Messages:
    267
    #5
    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. macrumors 68040

    Joined:
    Apr 22, 2005
    Messages:
    3,528
    #6
    Your example is OK, because the pointer doesn't actually point to a concrete instance of myObject until you explicitly allocate it.
     
  7. macrumors 6502

    Joined:
    Dec 17, 2009
    Messages:
    267
    #7
    As The King used to say, "thank you very much."
     
  8. macrumors 6502a

    Joined:
    Jun 27, 2010
    Messages:
    533
    #8
    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. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    Messages:
    1,896
    #9
    Note that the declaration
    Code:
    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. macrumors regular

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

    See?
     

Share This Page