|Mar 24, 2008, 01:25 AM||#1|
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;
|Mar 24, 2008, 01:35 AM||#2|
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 02:02 AM.
|Mar 24, 2008, 06:33 AM||#3|
b e n
|Mar 26, 2008, 12:31 PM||#4|
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.)
|Thread Tools||Search this Thread|
|thread||Thread Starter||Forum||Replies||Last Post|
|Best Reference Management Software||skaertus||Mac Applications and Mac App Store||7||Jul 1, 2014 06:37 PM|
|iPhone: just for future reference...||aPple nErd||Jailbreaks and iOS Hacks||2||May 5, 2013 11:00 PM|
|Anyone know of a (reference) app like this?||someone28624||iPad Apps||2||Nov 10, 2012 05:23 PM|
|October 1st reference in media event reference to next media event for iPad?||knemonic||iPad||7||Sep 13, 2012 03:42 PM|
|Reference material||LastName||iOS 6||1||Jun 14, 2012 09:54 AM|
All times are GMT -5. The time now is 12:20 PM.