So, I understand what pointers are, what it means to reference something, and have a vague understanding of this 'heap'. Where I begin to lose my grip on things is when we introduce functions and classes with the use of these concepts, e.g. sending pointers, returning pointers, etc.
If by reference and passing pointers essentially perform the same function, what is the advantage of using one over the other? Both essentially are passing by reference and manipulating the object outside of the called function.
So, by reference:
#include <iostream>
class student{
public:
int semesterHours;
float gpa;
};
void defRefS( student &refS );
void defPointerS( student *Ps );
void defValueS( student cS );
int main()
{
student s;
defRefS(s); //Here,
std::cout << "\nBack in main we have " << s.semesterHours << " hours";
std::cout << "\nBack in main the GPA is: " << s.gpa;
student *Ps = &s;
defPointerS(&s); //And here is where I get confused
std::cout << "\nBack in main we have " << s.semesterHours << " hours";
std::cout << "\nBack in main the GPA is: " << s.gpa;
defValueS(s);
std::cout << "\nBack in main object S has: " << s.semesterHours << " hours";
std::cout << "\nBack in main object S has: " << s.gpa << " GPA\n\n";
}
void defRefS( student &refS ) //Passing by reference to object
{
refS.gpa = 4.0;
refS.semesterHours = 12;
std::cout << "\n------------- Reference Function ------------";
std::cout << "\n\nObject S has: " << refS.semesterHours << " hours";
std::cout << "\nObject S has: " << refS.gpa << " GPA";
}
void defPointerS( student *Ps ) //Passing a pointer to the object
{
Ps->gpa = 5.0;
Ps->semesterHours = 14;
std::cout << "\n\n------------- Pointer Function ---------------";
std::cout << "\n\nNow object S has: " << Ps->semesterHours << " hours";
std::cout << "\nNow object S has: " << Ps->gpa << " GPA";
}
void defValueS( student cS ) //Passing the object by value
{
cS.gpa = 100;
cS.semesterHours = 50;
std::cout << "\n\n------------- Value Function ------------------";
std::cout << "\n\nObject S has: " << cS.semesterHours << " hours";
std::cout << "\nObject S has: " << cS.gpa << " GPA";
}
Passing by reference essentially allows the notation to be similar since, I suppose, in all ways refS
is the s
object. So, this leads to a simple way to manipulate an object using a function.
Passing a pointer is easy enough to understand. It's just a pointer to the object. Although how about this in the above code:
void defRefS( student &refS );
void defPointerS( student *Ps );
void defValueS( student cS );
Are all of these functions only defined to work with the student class? So, these functions are only able to pass reference, pointer, and values of objects to this specific class?
defRefS(s); //Here,
defPointerS(&s); //And here is where I get confused
defValueS(s);
If passing by reference, shouldn't you pass the address of an object? So, for me, it made more since to pass the argument of the pointer function in the reference function.
The function defPointerS
is defined to accept pointers; I'm sending it addresses?