简体   繁体   English

尝试学习课程和“堆” C ++

[英]Trying to learn classes and the 'heap' c++

So, I am pretty confused on the below code. 因此,我对以下代码感到非常困惑。 It its directly quoted from C++ for Dummies 5th ed, minus the comments I made. 它直接引用了C ++中的Dummies第五版,减去了我的评论。

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? 如果我正确地看待这个问题,是否使用新函数在堆中分配了两个新对象,它们分别具有名称“ student”和“ studentFriend”?

Then the object student.valFriend is being assigned all values that are of the object 'studentFriend.' 然后,将为对象Student.valFriend分配对象'studentFriend'的所有值。

The last bit of code appears to me to be assigning some undeclared member object of the class 'student' the reference of studentFriend? 在我看来,代码的最后一位似乎是为班级“学生”的一些未声明的成员对象分配了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? 一个简单的解决方法是,可能在文本中将其指定为“ student.ptrFriend”,但他们不会看到吗? 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. 尽管如此,它仍然令人困惑。

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 . student& student You want to name a variable student . 您要命名一个可变student But student is already a taken name (it's the name of your class). 但是student已经是一个取名了(这是班级的名字)。 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: 因此,如果struct / class包含引用,则在构造对象时必须对其进行初始化:

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 ? 使用delete &myStudent吗? This is wrong on many, many levels. 这在许多很多层面上都是错误的。 It's better to use pointer to dynamic memory or use automatic storage objects: 最好使用pointer动态内存的pointer或使用自动存储对象:

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. 尽管您的样式会产生有效的C ++代码,但我永远不会使用它,因为很难发现错误。 Find a good book, understand references, pointers and dynamic memory. 找到一本好书,了解参考书,指针和动态内存。

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. 第二:除了代码无效之外,切勿取消引用由newmalloc检索的指针-内存很可能会丢失(aka泄漏),因为您忘记以后将其删除。 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. 除此之外,请检查解释的堆栈和堆以及解释类,以获得更好的解释和深入的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM