简体   繁体   中英

C++ dynamic array of pointer to another class

Hello i'm trying to create a dynamic array of pointer to an object Student from Grades class but i can't figure out how to declare it in the header

that's the header:

class Grades
        {
private:
    Student** array;
    int _numofStud;


public:


    Grades();
    Grades(const Grades& other);
    ~Grades();

and the grades constructor (i'm not sure it's right)

Grades::Grades()
{
    this->array = new Student * [2];
    for (int i = 0; i < 2; ++i)
    {
        this->array[i] = NULL;
    }
    this->array[0]= new Student("auto1", "12345");
    this->array[1]= new Student("auto2", "67890");
    this->_numofStud = 2;
} 

The probleme is that before it even enter to the constructor, it creating me an array of Size 5 in Grades because i have 5 elements in the Student constructor

Student::Student(const char* name, char* id)
{
    this->_numofgrade = 0;
    this->setName(name);
    this->setId(id);
    this->_grades = NULL;
    this->_average = 0;
}

And i can't add or modify this size

I want to put a default size of Grades to an array of 2 pointers to student object that i'll define as default then i'll have an other methods that add new Students by creating them and adding their pointers to the array Th problem is i can't change the size of array and i don't understand why

I hope i was clear in my explanation thanks for your help

Edit: 在此处输入图片说明

that's the debuger and you can see when it's creating a new object Grades g1 it's creating an array of 5 instead off two fill the 2 first as i asked for and the 3 left i have no idea why they have been created and whats inside them

OK, so to be clear, in any actual programs you should use std::vector or other containers, they have a lot of features I ignored here (being templates, supporting move semantics, not requiring a default constructor, etc.), a lot of saftey (what if a constructor throws an exception? What if I do array.add(array[0]) ?), while still being pretty well optimised for general purpose usage.

And you should also really look at std::unique_ptr , manual new , delete , is generally asking for leaks and other mistakes, in C++ a manual "free" or "delete" of any resource is almost never needed.

Also note in C++ size_t is often used for sizes/lengths of objects and containers.

So the basic idea of a dynamic array is it changes it's size based on current requirements, so Grades() can just start off empty for example.

Grades::Grades()
    : array(nullptr), _numofStud(0)
{}

Then when adding a new item, a new larger array is made, and all the existing items are copied (roughly what std::vector::push_back(x) does).

void Grades::addStudent(Student *student)
{
    // make a larger array
    Student **newArray = new Student*[_numofStud + 1];
    // copy all the values
    for (int i = 0; i < _numofStud; ++i)
        newArray[i] = array[i]; // copy existing item
    // new item
    newArray[_numofStud] = student;
    ++_numofStud;
    // get rid of old array
    delete[] array;
    // use new array
    array = newArray;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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