So, I am pretty confused on the below code. It its directly quoted from C++ for Dummies 5th ed, minus the comments I made.
class student{
public:
int semesterHours;
float gpa;
student valFriend; //I thought to use the name of the class as a data type
student& refFriend;
student* ptrFriend;
}; //then this line should read .. '}student;' to enable something like the above
int main()
{
student& student = *new student; //new student object 'student'
student.gpa = 2.0;
student& studentFriend = *new student; //new student object 'studentFriend'
studentFriend.gpa = 4.0;
student.valFriend = studentFriend;
student.pFriend = &studentFriend;
}
Within the class 'student' are those objects of itself? How is that even possible? If I am looking at this correctly, are two new objects being allocated off the heap with the respective names 'student' and 'studentFriend' using the new function?
Then the object student.valFriend is being assigned all values that are of the object 'studentFriend.'
The last bit of code appears to me to be assigning some undeclared member object of the class 'student' the reference of studentFriend? Perhaps someone could explain this one a bit more explicitly. A simply fix is that possibly in the text it was meant to be 'student.ptrFriend,' but would they not have saw that? Maybe I'm the one missing something. Otherwise, I don't understand the syntax of this line.
On top of all this, the code does not even compile. What is going on here? I suppose the reason for not compiling could be because it is just a bit of code in a section about referencing and pointers. Nonetheless, it is still confusing.