 |
 |
Variable length argument lists
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2000
Status:
Offline
|
|
I'm working on a tree class, and I need to use variable length argument lists; however, my understanding of them is a little fuzzy. Will this snippet yield the expected result?
Code:
- (id)initWithObject:(id)object children:(id<ITreeNode>)firstChild, ...
{
va_list ap;
id *temp;
if(self = [super init])
{
ASSIGN(_object, object);
if(firstChild)
{
_children = [[NSMutableArray alloc] init];
va_start(ap, firstChild);
for(temp = firstChild; *temp != nil; temp++)
{
[_children addObject:*temp];
}
va_end(ap);
}
}
return self;
}
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2000
Status:
Offline
|
|
Do I even need to use the va_* stuff? The call stack should come in something like otherStuff, firstAddress, secondAddress, ..., nil shouldn't it? I think I'm just confusing myself more.
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2000
Status:
Offline
|
|
Decided that I really do need the va_* stuff. What if the stack grows in the opposite direction? 
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2001
Location: Sweden
Status:
Offline
|
|
Another solution would of course be:
Code:
- (id)initWithObject:(id)object children:(NSArray*)children
{
...
}
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Aug 2000
Location: UK
Status:
Offline
|
|
Check out the GNUStep implementation of -[NSArray initWithObjects]:
link
|
|
|
| |
|
|
|
 |
|
 |
|
Dedicated MacNNer
Join Date: Nov 2000
Status:
Offline
|
|
Thanks for the link. I had already consulted that file actually. My original attempt was a synthesis of _initWithObjects: rest: and K&R's minprintf example.
Figured out my problem. I'll post the code in case anyone else ever has questions about this.
Code:
- (id)initWithObject:(id)object children:(id<ITreeNode>)firstChild, ...
{
va_list ap;
id child;
NSMutableArray *children;
if(self = [super init])
{
if(firstChild)
{
children = [[NSMutableArray alloc] init];
AUTORELEASE(children);
va_start(ap, firstChild);
for(child = firstChild; child != nil; child = va_arg(ap, id))
{
[children addObject:child];
}
va_end(ap);
}
self = [self initWithObject:object childArray:children];
}
return self;
}
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |