Thursday, December 19, 2024
HomeComputer programmingC programmingC++: Pointers, Pass by Value, Pass by Reference

C++: Pointers, Pass by Value, Pass by Reference

You just cannot properly explain pointers without first knowing the basics of passing by value/reference.


I am sure someone 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 either case, the call will look the same. It is the receiving method which determines how the pass is made.

By Value:

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:

void aMethod(aParameter& p) { }

NOTE: The & can be placed 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 the following:
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 were a structure, class, or template, etc., it will have fields that are accessible 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 and accessed in a fashion 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 its 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 to which i points) The * operator in both cases are NOT the same.
-The first is a pointer reference belonging to the type. In this case, it is int.
-The second is a de-referencing operator belonging to the variable which resolves it into the variable to which it is pointing.

Keep in mind that pointers are limitless:
int**** i = new int(1);
cout<<****i; All that is needed is to de-reference as many times as is originally referenced, but you are probably thinking, "Why would anyone EVER do that?" The quickest answer is for arrays. Arrays are complicated and cumbersome to code. If there is a faster and easier way, we should do it. 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 the following: sizeOf and overloading the [] operator.
I will not discuss why or how this works. Maybe when I write the [] overloading function, I will explain it. Just know that it does.

Examples:
int* i = new int[3]; // (defines a pointer to an array of three ints)
cout<

Previous article
Next article
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!