Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Mar 24, 2008, 12:25 AM   #1
zcarter
macrumors member
 
Join Date: Apr 2007
Pass by Reference

How do you pass by reference in OOC for the iPhone?

For example, in C++ if I wanted to get the variable rows out of this function I would do something like this...

(Very down to earth)

void Function(int& rows)
{
rows = 10;
}
__________________
Zachary Carter
Co-Founder
support@iphonedevcentral.org
http://www.iphonedevcentral.org
zcarter is offline   0 Reply With Quote
Old Mar 24, 2008, 12:35 AM   #2
admanimal
macrumors 68040
 
Join Date: Apr 2005
Well the first thing to keep in mind is that references in C++ are just pointers with some sugar on them that allow you to do stuff without dealing with pointer semantics. So if you want to achieve the effect of passing an argument by reference, you just have the argument be a pointer to whatever you want to alter.

An example from an iPhone AppDelegate:

- (void)applicationDidFinishLaunching: (UIApplication *)application

application is effectively passed by reference in that whatever operations you do to application inside of this function will persist when it returns (i.e. you are operating on the original object not a copy of it).

So your example would be:

void Function(int* rows)
{
*rows = 10;
}

and when you call the function you would do it with Function(&x) where x is an int. Don't confuse the address-of operator (&) with the reference sign in C++, their use is somewhat different.

If you are dealing with objects rather than primitives, you don't really need to worry about it since the normal way of doing things in Obj-C is to only use pointers to objects, so the natural way of coding in Obj-C already has the effect of passing objects by reference.

Last edited by admanimal; Mar 24, 2008 at 01:02 AM.
admanimal is offline   0 Reply With Quote
Old Mar 24, 2008, 05:33 AM   #3
lazydog
macrumors 6502a
 
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
Quote:
Originally Posted by zcarter View Post
How do you pass by reference in OOC for the iPhone?

For example, in C++ if I wanted to get the variable rows out of this function I would do something like this...

(Very down to earth)

void Function(int& rows)
{
rows = 10;
}
I do believe you can do this by using the Objective-C++ compiler. All you need to do is give your file a .mm extension.

b e n
lazydog is offline   0 Reply With Quote
Old Mar 26, 2008, 11:31 AM   #4
zippyfly
macrumors regular
 
Join Date: Mar 2008
ObjC

I don't know C++ but for C and Objective-C you pass by reference using pointers.

int *myPntr = &value;

now myPntr contains the address where value holds its data.

If you send myPntr to a function (or method) and the method modifies the underlying data when you resolve myPntr using *myPntr (the dereferencing operator) the data is permanently modified. It is not duplicated and limited to the variable scope of the method routine. So when it returns from the method, the data is already modified. You do not even have to return the value (and you can only return one value anyway). So probably you can just return the boolean TRUE for a mission accomplished.

You can pass multiple such pointers to methods that take pointers as arguments. Note that ObjC can also take these as objects (which are actually just pointers, to be technical about it) and the object members (the values) will also be permanently modified by the method.

(Caveat: I am just starting to learn Objective-C actually but I think the above is correct.)
zippyfly 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
Best Reference Management Software skaertus Mac Applications and Mac App Store 7 Jul 1, 2014 05:37 PM
iPhone: just for future reference... aPple nErd Jailbreaks and iOS Hacks 2 May 5, 2013 10:00 PM
Anyone know of a (reference) app like this? someone28624 iPad Apps 2 Nov 10, 2012 04:23 PM
October 1st reference in media event reference to next media event for iPad? knemonic iPad 7 Sep 13, 2012 02:42 PM
Reference material LastName iOS 6 1 Jun 14, 2012 08:54 AM

Forum Jump

All times are GMT -5. The time now is 10:42 AM.

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

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC