【问题标题】:Why pointer to pointer was used in the code?为什么在代码中使用指向指针的指针?
【发布时间】:2021-07-07 16:36:44
【问题描述】:

为什么在代码中使用了指向指针而不是单指针?另外你认为析构函数写错了,如果我怎样才能使它正确?

指向指针的指针:employee** _arr;

你可以看到下面的代码:

#include<iostream>


class employee {
private:
    std::string _name;
    std::string _surname;
    int _year;
    double _salary;
    static int numberOfEmployees;
public:

    employee() {
        _name = "not-set";
        _surname = "not-set";
        _year = 0;
        _salary = 0;

        numberOfEmployees++;
    }
    employee(int year, std::string name, std::string surname) {
        _name = name;
        _surname = surname;
        _year = year;

        numberOfEmployees++;
        calculateSalary();
    }
    void calculateSalary() {

        //salary = 2310 + 2310 * year * 12/100.0
        _salary = 2310 + (2310 * (double)_year) * (12 / 100.0);
    }
    void printInfo() {
        std::cout << _name << " " << _surname << " " << _year << " " << " " << _salary << " TL/month" << std::endl;
    }

    static int getEmployeeCount() {
        return numberOfEmployees;
    }
};

class employeeList {
private:
    int _size;
    int _lenght;
    employee** _arr;
public:
    employeeList() :_size(1), _lenght(0), _arr(NULL) {}
    employeeList(int size) :_size(size) {
        _arr = new employee * [_size];
        _lenght = 0;
    }
    int listLength() {
        return _lenght;
    }
    employee retrieve_employeeFromIndex(int index) {
        if (index >= 0 && index < _size) {
            return *_arr[index];
        }
    }
    void addToList(employee* item) {
        _lenght++;
        if (_lenght <= _size) {
            _arr[_lenght - 1] = item;
        }
        else {
            std::cout << "you cannot add another employee!";
        }
    }
    static void printEmployees(employeeList el) {
        for (int i = 0; i < el._lenght; i++) {
            el._arr[i]->printInfo();
        }
    }
    ~employeeList() {
        delete[]  _arr;
    }
};
int employee::numberOfEmployees = 0;


int main() {


    employee a;
    employee b(5, "John", " Doe");
    employee c(3, "Sue", "Doe");



    employeeList empList(employee::getEmployeeCount());


    empList.addToList(&a);
    empList.addToList(&b);
    empList.addToList(&c);



    employeeList::printEmployees(empList);
    std::cout << empList.listLength() << std::endl;



    return 0;
}

你可以看到输出:

为什么在代码中使用了指向指针而不是单指针?另外你认为析构函数写错了,如果我怎样才能使它正确?

【问题讨论】:

  • 为什么在代码中使用指向指针的指针而不是单个指针? -- 代码中不需要任何指针。 std::vector&lt;employee&gt;employeList,而 static int numberOfEmployees; 不再需要。
  • 将指向本地对象的指针放入列表中...灾难的秘诀。
  • 看看你的retrieve_employeeFromIndex函数。如果索引超出范围,你会返回什么?
  • 要回答原始问题,您需要指向对象的指针数组,而不是指向单个对象的指针。

标签: c++ pointers destructor pointer-to-pointer


【解决方案1】:

我不是专家,但你会搞砸你的话题:P 我认为这个问题并不精确。你的意思是指向指针的指针:? 员工** _arr; 因为是指向一个指针: _arr = 新员工 * [_size]; 我认为这是有道理的,因为数组是一个指针?当然,我可能是错的,因为我刚开始接受教育。 为什么你认为 destruktor 是错误的?它正在删除一个指针。

【讨论】:

  • 你为什么认为 destruktor 是错的?它正在删除一个指针。 这是真的。该对象将包含一个指向指针数组的指针,当前指针数组指向的对象是自动分配的,不需要deleteed。不幸的是,使用自动分配不能很好地扩展,当它合适时,不需要动态分配数组。它的大小是已知的,由要添加到它的自动分配的已知数量固定。所以目前它是正确的,但可能不会长期正确。
【解决方案2】:

为什么在代码中使用了指向指针的指针?

这只有编写代码的作者知道。我们可以合理猜测他们的意图可能是:

  1. 使用指向该数组第一个元素的裸指针分配对象的动态数组。
  2. 间接指向存储在其他地方的对象,因此他们想使用指针数组,因此指向数组第一个元素的指针就是指向指针的指针。

他们的选择 1. 使用拥有裸指针是不必要的,并且有更好的选择不需要拥有裸指针。最常见的是,std::vector 将用于创建动态数组。

他们的选择 2. 间接指向不属于类实例的对象并不像让类实例拥有对象那样安全,但无论如何这可能是一个合理的选择,具体取决于他们选择的原因这个设计。如果没有程序应该做什么的文档,就不可能判断这个选择是否好。根据类的通用名称,我怀疑这不是一个好的选择。

你认为析构函数写错了吗

可以认为是正确的。不过,该课程还有其他问题。

整个employeeList 类似乎毫无意义,很容易被std::vector 替换。 printEmployees 是唯一一个不会由向量直接提供的成员函数。您可以为此使用非成员函数。

【讨论】:

    猜你喜欢
    • 2012-10-07
    • 2012-08-20
    • 2011-12-16
    • 1970-01-01
    • 2021-12-04
    • 2018-02-09
    • 1970-01-01
    • 2012-07-23
    • 2017-08-26
    相关资源
    最近更新 更多