C ++:数组的setter和getters

问题描述 投票:-1回答:1

我正在努力寻找正确的格式来初始化类中的(私有)数组并从类外获取/设置值。

我的代码是半功能的,但在格式不正确时感觉很尴尬。它只返回数组的第一个元素,我希望它返回所有内容。阅读代码注释以获取更多详细信

注意:这是我正在为学校工作的项目(很小的一部分) - 必须使用数组,而不是向量或列表。


student.h

class Student {
public:
    // Upon researching my issue, I read suggestions on passing pointers for arrays:
    void SetDaysToCompleteCourse(int* daysToCompleteCourse[3]);
    int* GetDaysToCompleteCourse(); // Ditto @ above comment.
private:
    int daysToCompleteCourse[3];

student.cpp

#include "student.h"

void Student::SetDaysToCompleteCourse(int* daysToCompleteCourse) {
    // this->daysToCompleteCourse = daysToCompleteCourse; returns error (expression must be a modifiable lvalue)
    // Feels wrong, probably is wrong:
    this->daysToCompleteCourse[0] = daysToCompleteCourse[0];
    this->daysToCompleteCourse[1] = daysToCompleteCourse[1];
    this->daysToCompleteCourse[2] = daysToCompleteCourse[2];
}

int* Student::GetDaysToCompleteCourse() {
    return daysToCompleteCourse;
}

ConsoleApplication1.cpp

#include "pch.h"
#include <iostream>
#include "student.h"

int main()
{
    Student student;
    int daysToCompleteCourse[3] = { 1, 2, 3 };
    int* ptr = daysToCompleteCourse;
    student.SetDaysToCompleteCourse(ptr);
    std::cout << *student.GetDaysToCompleteCourse(); // returns first element of the array (1).
}

我给了我最好的投篮机会,但我认为我需要在正确的方向上轻推一下。这里的任何提示将不胜感激。

c++ arrays getter-setter
1个回答
1
投票

我会说:

// student.h
class Student
{
public:
    // If you can, don't use numbers: 
    // you have a 3 on the variable,
    // a 3 on the function, etc.
    // Use a #define on C or a static const on C++
    static const int SIZE= 3;
    // You can also use it outside the class as Student::SIZE

public:
    void SetDaysToCompleteCourse(int* daysToCompleteCourse);

    // The consts are for "correctness"
    // const int* means "don't modify this data" (you have a setter for that)
    // the second const means: this function doesn't modify the student
    // whithout the const, student.GetDaysToCompleteCourse()[100]= 1 is 
    // "legal" C++ to the eyes of the compiler
    const int* GetDaysToCompleteCourse() const; // Ditto @ above comment.

    Student()
    {
        // Always initialize variables
        for (int i= 0; i < SIZE; i++) {
            daysToCompleteCourse[i]= 0;
        }
    }

private:
    int daysToCompleteCourse[SIZE];
    // On GCC, you can do
    //int daysToCompleteCourse[SIZE]{};
    // Which will allow you not to specify it on the constructor
};

// student.cpp
void Student::SetDaysToCompleteCourse(int* newDaysToCompleteCourse) 
{
    // It's not wrong, just that
    // this->daysToCompleteCourse[0] = daysToCompleteCourse[0];
    // use another name like newDaysToCompleteCourse and then you can suppress this->
    // And use a for loop
    for (int i= 0; i < SIZE; i++) {
        daysToCompleteCourse[i]= newDaysToCompleteCourse[i];
    }
}

const int* Student::GetDaysToCompleteCourse() const
{
    return daysToCompleteCourse;
}


// main.cpp
#include <iostream>

std::ostream& operator<<(std::ostream& stream, const Student& student)
{
    const int* toShow= student.GetDaysToCompleteCourse();
    for (int i= 0; i < Student::SIZE; i++) {
        stream << toShow[i] << ' ';
    }
    return stream;
}

int main()
{
    Student student;
    int daysToCompleteCourse[3] = { 1, 2, 3 };
    // You don't need this
    //int* ptr = daysToCompleteCourse;
    //student.SetDaysToCompleteCourse(ptr);
    //You can just do:
    student.SetDaysToCompleteCourse(daysToCompleteCourse);

    // On C++ int* is "a pointer to an int"
    // It doesn't specify how many of them
    // Arrays are represented just by the pointer to the first element
    // It's the FASTEST and CHEAPEST way... but you need the SIZE
    const int* toShow= student.GetDaysToCompleteCourse();
    for (int i= 0; i < Student::SIZE; i++) {
        std::cout << toShow[i] << ' ';
        // Also works:
        //std::cout << student.GetDaysToCompleteCourse()[i] << ' ';
    }
    std::cout << std::endl;

    // Or you can do: (because we defined operator<< for a ostream and a Student)
    std::cout << student << std::endl;
}

你可以在这里看看它:https://ideone.com/DeJ2Nt

© www.soinside.com 2019 - 2024. All rights reserved.