简体   繁体   English

C++ 指向另一个类的指针的动态数组

[英]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您好,我正在尝试从Grades类创建一个指向对象Student的动态指针数组,但我不知道如何在标题中声明它

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该probleme是之前就进入到构造函数,它创造了我5号的阵列中的Grades ,因为我在学生构造5种元素

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我想将一个默认大小的 Grades 放在一个包含 2 个指向学生对象的指针的数组中,我将其定义为默认值,然后我将有其他方法通过创建新学生并将他们的指针添加到数组中来添加新学生 问题是我无法改变数组的大小,我不明白为什么

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那是调试器,你可以看到当它创建一个新对象 Grades g1 它正在创建一个 5 的数组而不是两个按照我的要求先填充 2,剩下的 3 我不知道它们为什么被创建以及它们里面是什么

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.好的,要明确一点,在任何实际程序中,您都应该使用std::vector或其他容器,它们有很多我在这里忽略的功能(模板、支持移动语义、不需要默认构造函数等),一个很多安全性(如果构造函数抛出异常怎么办?如果我执行array.add(array[0])怎么办?),同时仍然针对通用用途进行了很好的优化。

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.而且您还应该真正查看std::unique_ptr ,手动newdelete ,通常要求泄漏和其他错误,在 C++ 中几乎从不需要手动“释放”或“删除”任何资源。

Also note in C++ size_t is often used for sizes/lengths of objects and containers.另请注意,在 C++ 中size_t通常用于对象和容器的大小/长度。

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::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).然后当添加一个新项目时,会创建一个新的更大的数组,并复制所有现有项目(大致是std::vector::push_back(x)所做的)。

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;
}

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

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