C ++:数组的Setter和Getters

时间:2019-03-16 01:29:17

标签: c++ arrays getter-setter

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

我的代码是半功能的,但是格式错误会感到尴尬。 它仅返回数组的第一个元素,我希望它返回所有内容。阅读代码注释以获取更多详细信息。

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


student.h

execute

student.cpp

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

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

我尽了最大努力,但我认为我需要朝着正确的方向轻推。 这里的任何提示将不胜感激。

1 个答案:

答案 0 :(得分: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