简体   繁体   English

具有动态数组和结构的函数中的引用C ++

[英]References in functions with dynamic arrays and structures C++

I'm having trouble understanding the theory between what is happening with my two functions in C++. 我很难理解C ++中两个函数之间发生的理论。 Unfortunately, I was unable to copy the whole code, because I would have to translate all of it from my native language to English. 不幸的是,我无法复制整个代码,因为我必须将所有代码从母语翻译成英语。 the dilemma I have here is the following: - both allStudents and oldAnswers are dynamic arrays - the function dataEntry works pefectly fine the way it is, it changes the allStudents, and the change is effective in the main function, although the arguments were dataEntry (Student * allStudents...) and not dataEntry (Student *& allStudents...) - in order to get the function addNewAnswer to effectively change the pointer oldAnswers in main function, I have to define the arguments with &, so addNewAnswer (AllAnswers *& oldAnswers...) 我在这里遇到的难题如下:-allStudents和oldAnswers都是动态数组-函数dataEntry完全按原样运行,它更改了allStudents,并且该更改在主函数中有效,尽管自变量是dataEntry( Student * allStudents ...)而不是dataEntry(Student *&allStudents ...)-为了获得函数addNewAnswer以有效地更改主函数中的指针oldAnswers,我必须使用&定义参数,因此addNewAnswer(AllAnswers *&oldAnswers ...)

Why does one work without the & and the other one doesn't, although the both effectively make changes to the pointers? 为什么在没有&的情况下一个有效,而在两者都有效地更改了指针的情况下,另一个却不起作用? Is it because the function addNewAnswer also makes changes to the arrays size (memory allocation)? 是否因为函数addNewAnswer也更改了数组大小(内存分配)?

 int questionsCounter = 5;
    enum Answers { CORRECT, INCORRECT };

    struct Student {
        int _stNumber;
        char _name[30];
        int _year;
        Answers *_answers;
        char * _userName;
        char *_password;
    };

    struct AllAnswers {
        int AnswerNumber;
        char *Question;
        Answers correctAnswer;
    };

    void dataEntry(Student * allStudents, int max) {

    for (int i = 0; i<max; i++) {
        cout << "\t::STUDENT " << i + 1 << "::";
        cout << "Enter Student's name: ";
        cin.getline(allStudents[i]._name, 30);
        cout << "Enter Student's number: ";
        cin >> allStudents[i]._stNumber;
        cout << "Enter Student's year (1,2,3,4): ";
        cin >> allStudents[i]._year;


        allStudents[i]._userName = new char[11];
        allStudents[i]._userName = GetUserName(allStudents[i]);

        allStudents[i]._password = nullptr;
        changePassword(allStudents[i]);     
    }
}

void addNewAnswer (AllAnswers *& oldAnswers, AllAnswers newAnswer) {

    AllAnswers *temp = new AllAnswers[questionsCounter+1];


    for (int i = 0; i < questionsCounter; i++)
    {   
        copyAnswer(oldAnswers[i], temp[i]);
    }

    copyAnswer(newAnswer, temp[questionsCounter]);

    deallocateAnswers(oldAnswers);

    assert(oldAnswers != NULL);

    oldAnswers = new AllAnswers[questionsCounter+1];


    for (int i = 0; i < questionsCounter+1; i++)
    {
        copyAnswer(temp[i], oldAnswers[i]); 

    }

    questionsCounter++; 
}

oldAnswers = edits the pointer itself, like you said. 如您所说, oldAnswers =编辑指针本身。

operator[] is actually a function which takes a pointer and returns a reference. operator[]实际上是一个函数,它接受一个指针并返回一个引用。 When you have allStudents[i] = , you're actually not editing the pointer itself. 当您拥有allStudents[i] = ,您实际上并不是在编辑指针本身。 Instead, you are traveling i Students past the allStudents pointer and editing whatever is there. 相反,您是在i Students经过allStudents指针并编辑其中的所有内容时旅行的。

So it doesn't matter if allStudents is a copy (pass by reference) or an alias (pass by value) of another pointer, because in either case all students[i] returns the same reference to the same Student . 因此, allStudents是另一个指针的副本(按引用传递)还是别名(按值传递)都没有关系,因为在任何情况下,所有students[i]将相同的引用返回给相同的Student

When not doing coursework, use std::vector instead of new[] . 当不做功课时,请使用std::vector而不是new[] It's a lot more intuitive with regards to value/reference semantics than a pointer into the heap, which comes in handy. 关于值/引用语义的直观性比进入堆的指针要直观得多,这很方便。

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

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