PDA

View Full Version : Does Obj-C Allow for Recursive Pointers?




BadWolf13
Oct 19, 2010, 02:11 PM
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?



iSee
Oct 19, 2010, 02:26 PM
There's no problem with having an instance of a class point to an instance of the same class.

lee1210
Oct 19, 2010, 02:48 PM
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

iSee
Oct 19, 2010, 03:59 PM
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).

BadWolf13
Oct 19, 2010, 04:49 PM
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?

admanimal
Oct 19, 2010, 04:59 PM
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?

Your example is OK, because the pointer doesn't actually point to a concrete instance of myObject until you explicitly allocate it.

BadWolf13
Oct 19, 2010, 05:13 PM
As The King used to say, "thank you very much."

SidBala
Oct 19, 2010, 06:50 PM
Thank you iSee. C++ is my background. I was pretty sure I wasn't allowed to do that in that language.

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.

Sydde
Oct 19, 2010, 07:29 PM
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?

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 (http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmObjectOwnership.html#//apple_ref/doc/uid/20000043-BEHDEDDB) - 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.

zeppenwolf
Oct 20, 2010, 10:44 PM
From _The C Programming Language_, K&R, Section 6.5, "Self-Referential Structures":



struct tnode {
char *word;
int count;
struct tnode *left;
struct tnode *right;
};
This recursive declaration of a node might look chancy, but it's correct. It is illegal for a structure to contain an instance of itself, but

struct tnode *left;

declares left to be a pointer to a tnode, not a tnode itself.

See?