Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 19, 2010, 02:11 PM   #1
BadWolf13
macrumors 6502
 
Join Date: Dec 2009
Does Obj-C Allow for Recursive Pointers?

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?
BadWolf13 is offline   0 Reply With Quote
Old Oct 19, 2010, 02:26 PM   #2
iSee
macrumors 68030
 
iSee's Avatar
 
Join Date: Oct 2004
There's no problem with having an instance of a class point to an instance of the same class.
__________________
"Nobody ever reads these things so I can write anything. I'd eat bananas every day if they were crunchy."
iSee is offline   0 Reply With Quote
Old Oct 19, 2010, 02:48 PM   #3
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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.

-Lee
lee1210 is offline   0 Reply With Quote
Old Oct 19, 2010, 03:59 PM   #4
iSee
macrumors 68030
 
iSee's Avatar
 
Join Date: Oct 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++)
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).
__________________
"Nobody ever reads these things so I can write anything. I'd eat bananas every day if they were crunchy."
iSee is offline   0 Reply With Quote
Old Oct 19, 2010, 04:49 PM   #5
BadWolf13
Thread Starter
macrumors 6502
 
Join Date: Dec 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?

Last edited by dejo; Oct 19, 2010 at 07:03 PM. Reason: Consecutive posts. Please use multi-quote.
BadWolf13 is offline   0 Reply With Quote
Old Oct 19, 2010, 04:59 PM   #6
admanimal
macrumors 68040
 
Join Date: Apr 2005
Quote:
Originally Posted by BadWolf13 View Post
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.
admanimal is offline   0 Reply With Quote
Old Oct 19, 2010, 05:13 PM   #7
BadWolf13
Thread Starter
macrumors 6502
 
Join Date: Dec 2009
As The King used to say, "thank you very much."
BadWolf13 is offline   0 Reply With Quote
Old Oct 19, 2010, 06:50 PM   #8
SidBala
macrumors 6502a
 
Join Date: Jun 2010
Quote:
Originally Posted by BadWolf13 View Post
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.

Last edited by SidBala; Oct 19, 2010 at 07:04 PM.
SidBala is offline   0 Reply With Quote
Old Oct 19, 2010, 07:29 PM   #9
Sydde
macrumors 68000
 
Sydde's Avatar
 
Join Date: Aug 2009
Quote:
Originally Posted by BadWolf13 View Post
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
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.
__________________
You got to be a spirit. You can't be no ghost.
Sydde is offline   0 Reply With Quote
Old Oct 20, 2010, 10:44 PM   #10
zeppenwolf
macrumors member
 
Join Date: Nov 2009
From _The C Programming Language_, K&R, Section 6.5, "Self-Referential Structures":

Quote:
Code:
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
Code:
    struct tnode *left;
declares left to be a pointer to a tnode, not a tnode itself.
See?
__________________
Maximum 200 characters and 4 lines.
No blank lines or lines of punctuation characters!
May not be distracting, oversized, or in conflict with any forum rules.
Please use continuous text rather t...
zeppenwolf is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Problem with Data usage recursive chezbob iPhone Tips, Help and Troubleshooting 2 Mar 27, 2014 06:18 PM
Need some buying/selling pointers Preed08 MacBook Pro 3 Aug 23, 2013 12:14 PM
about recursive looping Sydde Mac Programming 3 Apr 29, 2013 10:05 PM
Pointers in C... can anyone help me understand? ravenvii Mac Programming 46 Feb 6, 2013 02:10 PM
Two (non-mirror) Screens? Any pointers? ArtOfWarfare iPhone/iPad Programming 3 Nov 12, 2012 12:17 AM

Forum Jump

All times are GMT -5. The time now is 03:34 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps