![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
![]() |

|
| Programming tutorials All Knowledge Info and links to posted here |
![]() |
|
C++: Pointers, Pass by Value, Pass by Reference
|
LinkBack | Thread Tools | Display Modes |
|
|
#1 (permalink) |
|
Administrator
Posts: 876
Join Date: Oct 2005
Rep Power: 10
IM:
|
I'm sure the odd person who reads this will say "but you can also pass a pointer" while this is all well and true, the pointer passed is still a primitive variable and will in turn either be passed by reference or by value. When passing a value, in eithe case, the call will look the same, it's the recieving method which determines how the pass is made. By Value: Code:
void aMethod(aParameter p) { }
By Reference: void aMethod(aParameter& p) { } NOTE: the & can be place either against the parameter type, against the parameter name, or there can be a space on either side. This essentially passes a pointer, but not the same kind of pointer you would get when creating a: Variable* v; That is why it is called by reference, since that is what you have, p now acts as if it were the parameter just like in the by value way, but any changes affect the original and stay changed outside of the aMethod scope. *Now if that parameter was a structure, class, or template, etc. It will have fields that are accesable by reference . or pointers -> In both cases of the above passing, the parameter passed can be accessed using the . or dot operator. Examples: *assuming fields within the class type. Person person; //person is an object of class Person person.name person.Chlid.name *It can also be used to call methods within the type. person.getFriends() //which would likely return a vector or array If the methods above were constructors or other member functions, accessed in a fasion like the method call just above, then there is a this operator for accessing the object which called it. Who's fields can also be accessed, but only by pointers. Since the object is passed implicitly it is passed as a pointer reference by default. Examples: -this now represents a pointer to a Person object this->name person->Child.name //if child can access it's name by reference it stays that way Once again this can be done with methods. this->getFriends() Other examples requiring pointers: int* i = new int(0); //this defines a dynamic int in memory pointed to by i cout<<*i; would print the value that i points to The * operator in both cases are NOT the same!! -the first is a pointer reference, belonging to the type, in this case int. -the second is a de-referencing operator belonging to the variable which resolves it into the variable it is point to. Keep in mind that pointers are limitless: int**** i = new int(1); cout<<****i; All that is needed is to dereference as many times as is originally referenced... but you're probably thinking, "why would anyone EVER do that?" Quick answer: arrays!! Arrays are complicated and cumbersome to code, if there is a faster and easier way shouldn't we do it? The answer is YES!! By defining arrays by pointer, they are dynamic, and can be created at run time, not based on static values, they can also be traversed in the same way thanks to the C++ ingenuity of: sizeOf and overloading the [] operator. I will not discuss why or how this works, maybe when i write the [] overloading function, just know that it does. Examples: int* i = new int[3]; //defines a pointer to an array of 3 ints cout<<i[0]; //returns the value at i[0] It may seem strange, but it is a little complex for this recipe, and involves the stack, bytes, and other pointers. This again is limitless: int** i = new int*[3]; //define a pointer to an array of pointers to ints cout<<*i[0]; //now we must dereference the final referencing that is not array specific. Play around with pointers, they are the best part of C and C++, even though they can cause the most headaches, when a * is missplaced. It opens up a whole new range of storage and arraging possiblities. |
|
|
|
|
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| King of Dragon Pass | SEO | Game Cheats | 0 | 01-Apr-2008 12:11 AM |
| Wii looks to pass Xbox 360 sales soon | Anilrgowda | wii news | 0 | 14-Aug-2007 12:21 AM |
| Flash- Pass URL variable into flash movie. | Sangeetha | Knowledge Base | 1 | 27-Feb-2007 03:53 AM |
| Pointers Ow Can They Work | kingaff | Programming tutorials | 0 | 23-Feb-2007 08:07 AM |
| Pass Variables to a New Thread in C# | Anilrgowda | Programming tutorials | 0 | 15-Dec-2006 01:21 AM |