2

I am brand new to C++ and am struggling with the idea of using arrays and returning pointers.

I have a class Student in a .h file:

class Student {
    public:
        // stuff
        int* GetNumDaysPerCourse() const;
        // stuff

    private:
        //stuff
        int numDaysPerCourse[3];
        // stuff
};

And I am trying to implement it in a .cpp file:

int* Student::GetNumDaysPerCourse() const {
    return numDaysPerCourse;
}

But I am getting an error saying:

return value type does not match the function type

I am confused because looking at other questions, this looks like a valid way to do this.

So, how do I return a class member array using a getter in C++?

6
  • Do you want people to be able to change values of the array that the pointer points to? Commented Aug 13, 2021 at 18:18
  • 1
    Please try to create a proper minimal reproducible example to show us, and then copy-paste the full and complete build output of that example into your question. Commented Aug 13, 2021 at 18:19
  • 1
    error: invalid conversion from ‘const int*’ to ‘int*’ [-fpermissive] return numDaysPerCourse; is the error I see since it wasn't included in the question. Commented Aug 13, 2021 at 18:21
  • 1
    By the way, returning pointers to internal data could lead to problems if the caller doesn't use the pointer correctly (modify data it should not modify, go out of bounds, etc.). I recommend you use std::array<int, 3> for the array, and return either by constant reference or by value. Commented Aug 13, 2021 at 18:22
  • 1
    @Mr.Spock Okay. Then you will want option 2 from Remy Lebeau's answer Commented Aug 13, 2021 at 19:12

2 Answers 2

6

When I try to compile this code, I get this error:

error: invalid conversion from ‘const int*’ to ‘int*’

This is because GetNumDaysPerCourse() is const-qualified, so its this pointer is pointing at a const Student object, and thus its numDaysPerCourse member is treated as const data. You can't assign the address of const data to a pointer-to-non-const (without an explicit type-cast, that is, but don't do that), as that would grant permission for outside code to modify data that is (potentially) stored in read-only memory.

So, you need to either:

  • drop the const qualifier from GetNumDaysPerCourse():
public:
    ...
    int* GetNumDaysPerCourse();
    ...

private:
    ...
    int numDaysPerCourse[3];
    ...
};
int* Student::GetNumDaysPerCourse() {
    return numDaysPerCourse;
}
  • make GetNumDaysPerCourse() return a pointer-to-const:
public:
    ...
    const int* GetNumDaysPerCourse() const;
    ...

private:
    ...
    int numDaysPerCourse[3];
    ...
};
const int* Student::GetNumDaysPerCourse() const {
    return numDaysPerCourse;
}
  • combine the two approaches to provide both const and non-const access to the same array, depending on whether the object that GetNumDaysPerCourse() is being called on is mutable or read-only:
public:
    ...
    int* GetNumDaysPerCourse();
    const int* GetNumDaysPerCourse() const;
    ...

private:
    ...
    int numDaysPerCourse[3];
    ...
};
int* Student::GetNumDaysPerCourse() {
    return numDaysPerCourse;
}

const int* Student::GetNumDaysPerCourse() const {
    return numDaysPerCourse;
}
Sign up to request clarification or add additional context in comments.

Comments

2

If you don't need to modify the pointer after calling int *GetNumDaysPerCourse() const;

You can modify your class and your implementation as follows:

class Student {
public:
    // stuff
    const int *GetNumDaysPerCourse() const;
    // stuff

private:
    //stuff
    int numDaysPerCourse[3];
    // stuff
};

The .cpp file:

const int *Student::GetNumDaysPerCourse() const {
    return numDaysPerCourse;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.