Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Keep The TechnoWorldInc.com Community Clean: Read Guidelines Here.
Recent Updates
[Yesterday at 11:48:22 AM]

[Yesterday at 11:48:22 AM]

[Yesterday at 11:48:22 AM]

[Yesterday at 11:48:22 AM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[February 14, 2024, 02:00:39 PM]
Subscriptions
Get Latest Tech Updates For Free!
Resources
   Travelikers
   Funistan
   PrettyGalz
   Techlap
   FreeThemes
   Videsta
   Glamistan
   BachatMela
   GlamGalz
   Techzug
   Vidsage
   Funzug
   WorldHostInc
   Funfani
   FilmyMama
   Uploaded.Tech
   MegaPixelShop
   Netens
   Funotic
   FreeJobsInc
   FilesPark
Participate in the fastest growing Technical Encyclopedia! This website is 100% Free. Please register or login using the login box above if you have already registered. You will need to be logged in to reply, make new topics and to access all the areas. Registration is free! Click Here To Register.
+ Techno World Inc - The Best Technical Encyclopedia Online! » Forum » THE TECHNO CLUB [ TECHNOWORLDINC.COM ] » Programming Zone » C/C++/C#
 C++: Pointers, Pass by Value, Pass by Reference
Pages: [1]   Go Down
  Print  
Author Topic: C++: Pointers, Pass by Value, Pass by Reference  (Read 2814 times)
Taruna
Elite Member
*****



Karma: 13
Offline Offline

Posts: 845

Hi ALL


View Profile
C++: Pointers, Pass by Value, Pass by Reference
« Posted: December 27, 2006, 12:43:07 AM »


You just can't properly explain pointers, without first knowing the basics of passing by value/reference.

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) { }

When passing by value a temporary COPY is made in memory taking up space and making any changes to the variable useless outside of the aMethod scope. Once control returns to the calling procedure any changes to the variable will not be recognized, and you will still have the original variable.

By Reference:

Code:
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.

Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Copyright © 2006-2023 TechnoWorldInc.com. All Rights Reserved. Privacy Policy | Disclaimer
Page created in 0.089 seconds with 24 queries.