0

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.

trincot
  • 317,000
  • 35
  • 244
  • 286
beeks
  • 197
  • 1
  • 1
  • 5
  • 11
    Is this really sample code from a book? My first thoughts would be to ditch this book and read something else entirely; this sample code is terrible. – reko_t Feb 14 '12 at 08:33
  • I assume it is just a bunch of code that is related to the topic at hand. It doesn't make since and effectively does nothing. I was just confused on some of the syntax it used. Especially the objects of student within its own class definition? – beeks Feb 14 '12 at 08:36
  • Agreed, you'll need a different book. Or even go online. A good place to start could be http://www.cplusplus.com/doc/tutorial/. I don't think you can even have an object student as a field of a student object, as it would infinitely construct student objects (though you could have a pointer). – Pochi Feb 14 '12 at 08:36
  • 1
    `student& student = *new student;` What kind of a book is this?!?! [Here are good books](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Marlon Feb 14 '12 at 08:38
  • `student& student = *new student;` - yuck. – razlebe Feb 14 '12 at 08:38
  • Agreed. This sucks. The reason (at least the first I see) it doesn't compile is that Student can't contain an instance of Student. It can contain a pointer or reference to a Student. The compiler needs to know how big a Student is, and if it contains another Student, then it needs to be bigger than itself, which is impossible. – BoBTFish Feb 14 '12 at 08:39
  • 1
    C++ for Dummies 5th Edition. I will try a new approach then. I never really fancied this particular reference anyways. Thanks! – beeks Feb 14 '12 at 08:40
  • I totally agree, throw away this book. – fdlm Feb 14 '12 at 08:41
  • I suppose for fairness we don't know the context. It might be "Here's some awful code. We're going to learn what's wrong and how to fix it." But even if that's the case, it's probably a bad idea to show beginners code that bad, as they'll copy bits of it anyway. – BoBTFish Feb 14 '12 at 08:52
  • fdlm: If this code sample is from a book, I would burn it and bring the ashes into the deep of Mordor and throw them into Mount Doom. Even if I don't know the way. BoBTFish: Probably. I definitely hope so. The 'xxx for Dummies' weren't such bad sometimes. – Zeta Feb 14 '12 at 08:52
  • The nicest thing about "student& student = *new student;" is that it screems MEMORY LEAK in your face. I know it's just an example, but it might stick in the brain while actually programming. – stefaanv Feb 14 '12 at 08:52

2 Answers2

1

It won't compile for the following reasons:

class student{
public:
    int semesterHours;
    float gpa;
    student valFriend; // this line is full of evil!
    student& refFriend;
    student* ptrFriend;
};

Just think for a while. A student can save the value of a student, which can save a value of a student, which can save a value.... You're building an object of infinite size. You can't store an object inside of itself.

student& student. You want to name a variable student. But student is already a taken name (it's the name of your class). Also, you can't change a reference. Once you made a reference it's fixed. So if a struct/class contains a reference it must be initialized while constructing the object:

class student{
public:
    student(student& myFriend):refFriend(myFriend), ptrFriend(0){}
    // if a student has no friend, we say he's his own friend:
    student():refFriend(*this), ptrFriend(0){}        
    int semesterHours;
    float gpa;    
    student& refFriend;
    student* ptrFriend;
};

int main()
{
    student& myStudent = *(new student);   //new student object 'student'
    myStudent.gpa = 2.0;

    student& studentFriend = *new student; //new student object 'studentFriend'
    studentFriend.gpa = 4.0;    

    myStudent.ptrFriend = &studentFriend;
}

However, there are some really creepy things going on there. Why do you want do allocate dynamic memory and store a reference to it? How are you going to deallocate? Using delete &myStudent? This is wrong on many, many levels. It's better to use pointer to dynamic memory or use automatic storage objects:

class student{
public:
    student(student& myFriend):refFriend(myFriend){}
    student():refFriend(*this){}
    int semesterHours;
    float gpa;    
    student& refFriend;
    student* ptrFriend;
};

int main()
{
    student myStudent;
    myStudent.gpa = 2.0;

    student studentFriend;
    studentFriend.gpa = 4.0;

    myStudent.ptrFriend = &studentFriend;
}

Although your kind of style will result in valid C++ code, I would never ever use it since it's very hard to find errors. Find a good book, understand references, pointers and dynamic memory.

Zeta
  • 103,620
  • 13
  • 194
  • 236
0

If this comes from a book, look for something else, please. It's quite an achievement to get this many coding errors in so few lines ;)

First: you can't declare a member of an unknown class:

class student{
public:
    // ...
    student valFriend; //<- impossible, class student is not known, yet
    student& refFriend; // impossible without initialization constructor
    // ...
};    

second: besides the code being invalid, never dereference a pointer retrieved by new or malloc - the memory will most likely be lost (aka leak) because you forget to delete it later. You can do something like this for this specific sample:

student james;
student jim;

james.ptrFriend = &jim; // stores the pointer to jim

Other than that, please check Stack and Heap explained and Classes explained for better explanation and in-depth information.

Coder02
  • 260
  • 2
  • 8