微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决为什么在代码中使用了指向指针的指针?

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

指向指针的指针: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;
}

你可以看到输出

enter image description here

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

解决方法

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

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

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

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

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

你认为析构函数写错了吗

可以认为是正确的。不过这门课还有其他问题。

整个 employeeList 类看起来毫无意义,可以很容易地替换为 std::vectorprintEmployees 是唯一不会由向量直接提供的成员函数。您可以改用非成员函数。

,

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。