I would say:

// 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;
}

You can check out it live here: https://ideone.com/DeJ2Nt

Answer from Mirko on Stack Overflow
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 62855
setter and getter function for an array - C++ Forum
I'm still not sure what you mean by how does the program know what you entered. You will pass in an integer as an index and your routine will simply return the value at that index. ... For getter it is simply to do. You declare parameter that will specify element's index of array...
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ cplusplus-programming โ€บ 152877-char-array-getter.html
Char array getter
Perhaps you should see where the getter is being called to see if the functionality belongs in the class that knows the name. In any case, if the Name is declared within a class then a member function of that class can safely return a pointer to the char array, since the array will be in scope as long as the class is in scope.
Discussions

Accessor (Getter) function for a c string (character array) - GameDev.net Forums
Ok, so I am working on this for school. I am building a model business application in the console using C++.The application is modeled as a database/cashier program. I have โ€ฆ More on gamedev.net
๐ŸŒ gamedev.net
June 24, 2010
How do I write setters and getters for an array? (c++) - Stack Overflow
Im writing a class within c++, however I am not certain on how to create the setters and getters for the arrays (sorry for it being a basic question!) I am getting the following error: expected p... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to implement a pointer array in setters y getters C++
I'm having problems set the getters and setters in a pointer array, dunno how to do it, how can I solve this problem, here is the code: https://code.sololearn.com/cRu2 More on sololearn.com
๐ŸŒ sololearn.com
1
0
C++ Array Question
Before I give you the real answer, let me give a few comments: You don't want the ClassName:: prefix when declaring member functions inside the class declaration. Use this syntax when defining them in the .cpp file IMO (this isn't universal by any means), getters and setters are code smell, a sign that your class design might need to be re-thought. Arrays are second-class citizens of the C++ type system. Often, if you can't figure out how to do something with them, try using std::array or std::vector. But moving on ... what's the "right" answer? Well, there's two options. There's the traditional way, and there's the super-secret way you almost never see done. Let's start with the traditional way. As I said above, arrays are second-class citizens. They can't be function arguments, nor can they be return types. Instead, the traditional way of dealing with this is to pass a pointer: void foo(int* array, int size) { for ( int i = 0; i < size; i++ ) std::cout << array[i] << '\n'; } int array[5]; int* bar() { return array; } void baz() { foo(array, 5); foo(bar(), 5); } There's some magic syntax that let's this happen: C++ lets you treat pointers as if they point to the first element of an array, and arrays automatically "decay" to a pointer to their first element. The only problem is that the size of the array isn't passed along with this pointer, so you have to carry that information around somehow else (implicitly, or via a second parameter, or make a struct that holds both, or use a terminator in the array like C strings do... whatever your situation calls for). FYI, the syntax also allows for void foo(int array[], int size);, but this is exactly equivalent to the pointer syntax. It's really only there to hint to the programmer that it's an array. I also promised a super-secret way. While arrays can't be function arguments, references to arrays can be: void foo(int (&array)[5]) { for ( int i : array ) std::cout << array[i] << '\n'; } Isn't that "beautiful" syntax? If you notice, this only works with a specific array size, as it's encoded into the parameter type (which also lets you use the ranged-base for loop), so it's common to use a template to make it more generic: template void foo(int (&array)[Size]) { for ( int i : array ) std::cout << array[i] << '\n'; } So what about returning arrays? If you thought the above syntax was beautiful, check this out: int (&bar())[5] { return array; } Believe it or not, that's the declaration for a function that returns a reference to an array of 5 integers. It's a bit better if you use a type alias to clean it up: using array5string = std::string[5]; array5string& bar() { return array; } You could also use a template here, I think, to make the size more generic ... but I can't think of a situation where you'd want to and where a std::vector wouldn't be the better choice anyway. More on reddit.com
๐ŸŒ r/learnprogramming
6
1
September 7, 2018
Top answer
1 of 1
8

I would say:

// 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;
}

You can check out it live here: https://ideone.com/DeJ2Nt

๐ŸŒ
GameDev.net
gamedev.net โ€บ home โ€บ forums โ€บ for beginners โ€บ accessor (getter) function for a c string (character array)
Accessor (Getter) function for a c string (character array) - GameDev.net Forums
June 24, 2010 - The common pattern in C is to use 'caller allocated buffer' functions that look like this: bool (or size_t) (or char *) GetWhatever(char *buffer, size_t bufferSize); // where you write the return value into 'buffer' void SetWhatever(const char ...
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 1951230 โ€บ how-can-i-use-setters-and-getters-in-a-char-array-c
How Can I use setters and getters in a char array? c++ | Sololearn: Learn to code for FREE!
I'm so used to work with std::string, but, I'm not sure how to use a char array in the getters and setters; I have this little code: char name[10] // I think is the same as char* name void setName(char* n) { name = n; } // ERROR is not assignable. char* getName() const { return name; } Ass you can see, I'm not sure how to get and set the name properly, how can I solved this? ... You will have to use some of the built in functions for char strings like strcpy for setting: { strcpy(n, name); }
๐ŸŒ
Narkive
comp.lang.cpp.moderated.narkive.com โ€บ kJJgigot โ€บ accessor-functions-getter-for-c-string-character-array-members
Accessor Functions (getter) for C String (Character Array) Members
For your array members you can use: void BookData::setTitle(const char* title) { assert(title); // Make sure we do not get passed a NULL pointer strncpy(m_title, title, 14); // attn: constants used - not good m_title[13] = '\0'; // Make sure always null terminated } const char* BookData::getTitle() { return m_title; } Now, that's not too clumsy, or is it? br, Martin ยท -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated.
๐ŸŒ
Eclipse
bugs.eclipse.org โ€บ bugs โ€บ show_bug.cgi
319278 โ€“ Generate getters and setters handles arrays incorrectly
Bugzilla โ€“ Bug 319278 Generate getters and setters handles arrays incorrectly Last modified: 2012-02-23 11:33:52 EST ... This site is maintained for archival purposes only. Eclipse projects have transitioned to GitHub and Eclipse GitLab. Use the Projects search tool to locate your project and access its latest code ...
Find elsewhere
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ c++ array question
r/learnprogramming on Reddit: C++ Array Question
September 7, 2018 -

Obviously if I had some integer in a class and I wanted to create a getter function for it, it would look something like this:

public:
    int ClassName::getInt() {
        return theInt;
    }

private:
    int theInt = 10;

But what if I had an array stored in a class and I want to have a getter function for that?

public:
    // string ClassName::getArrayOfString() {
        return // What goes here/ is the line above correct?
    }

private:
    string ArrayOfStrings[2] = {"Bob", "Karen"};

Top answer
1 of 4
3
Before I give you the real answer, let me give a few comments: You don't want the ClassName:: prefix when declaring member functions inside the class declaration. Use this syntax when defining them in the .cpp file IMO (this isn't universal by any means), getters and setters are code smell, a sign that your class design might need to be re-thought. Arrays are second-class citizens of the C++ type system. Often, if you can't figure out how to do something with them, try using std::array or std::vector. But moving on ... what's the "right" answer? Well, there's two options. There's the traditional way, and there's the super-secret way you almost never see done. Let's start with the traditional way. As I said above, arrays are second-class citizens. They can't be function arguments, nor can they be return types. Instead, the traditional way of dealing with this is to pass a pointer: void foo(int* array, int size) { for ( int i = 0; i < size; i++ ) std::cout << array[i] << '\n'; } int array[5]; int* bar() { return array; } void baz() { foo(array, 5); foo(bar(), 5); } There's some magic syntax that let's this happen: C++ lets you treat pointers as if they point to the first element of an array, and arrays automatically "decay" to a pointer to their first element. The only problem is that the size of the array isn't passed along with this pointer, so you have to carry that information around somehow else (implicitly, or via a second parameter, or make a struct that holds both, or use a terminator in the array like C strings do... whatever your situation calls for). FYI, the syntax also allows for void foo(int array[], int size);, but this is exactly equivalent to the pointer syntax. It's really only there to hint to the programmer that it's an array. I also promised a super-secret way. While arrays can't be function arguments, references to arrays can be: void foo(int (&array)[5]) { for ( int i : array ) std::cout << array[i] << '\n'; } Isn't that "beautiful" syntax? If you notice, this only works with a specific array size, as it's encoded into the parameter type (which also lets you use the ranged-base for loop), so it's common to use a template to make it more generic: template void foo(int (&array)[Size]) { for ( int i : array ) std::cout << array[i] << '\n'; } So what about returning arrays? If you thought the above syntax was beautiful, check this out: int (&bar())[5] { return array; } Believe it or not, that's the declaration for a function that returns a reference to an array of 5 integers. It's a bit better if you use a type alias to clean it up: using array5string = std::string[5]; array5string& bar() { return array; } You could also use a template here, I think, to make the size more generic ... but I can't think of a situation where you'd want to and where a std::vector wouldn't be the better choice anyway.
2 of 4
2
Since an array is basically a pointer, and functions can't return arrays directly in c++, you can return a string pointer. I think the code looks like this: public: string* ClassName::getArrayOfStrings(){ return ArrayOfStrings; } private: string ArrayOfStrings[2] = {"Bob", "Karen"}
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ general โ€บ 111968
getter and setter for array type - C++ Forum
Another help would be for the top 5 score. can anyone guide me along how can I display 5 top score of a player? i'm really bad at using array =\ I understand that under the private sector I should probably define int topFivescore[5]. but if I assign that I cant seems to get my program to run.
๐ŸŒ
Reddit
reddit.com โ€บ r/cpp_questions โ€บ setter methods for a char array
r/cpp_questions on Reddit: Setter methods for a char array
February 3, 2021 -

Hi all, I am trying to write a setter for a char array but getting into issues in the compilation side.

This char array of mine is used for ingesting in the name of a course.

In my header file, I have the following:

class Course
{
    public:
        Course(const char *name, unsigned credits);
        void GetUnitName() const;
        void SetUnitName(char* buf);
    private:
        char cName[10];
        int cCredits;
};

inline void Course::GetName() const
{
    cout << "Course name is: " << cName << endl;
}

inline void Course::SetName(char *input)
{
    cName = input;
}

I got the following error:

`error: incompatible types in assignment of 'char*' to 'char [10]'`

I tried several ways such as giving the defined size `char input[10]` however that fails badly too.

Can someone kindly guide me?

๐ŸŒ
GameDev.net
gamedev.net โ€บ home โ€บ forums โ€บ for beginners โ€บ accessor (getter) function for a c string (character array)
Accessor (Getter) function for a c string (character array) - For Beginners - Forums - GameDev.net
June 24, 2010 - The common pattern in C is to use 'caller allocated buffer' functions that look like this: bool (or size_t) (or char *) GetWhatever(char *buffer, size_t bufferSize); // where you write the return value into 'buffer' void SetWhatever(const char ...
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 271994
Is it possible to create a getter for a private array?
You can declare class B as a friend of class A, or you can provide a function that grants access to each array element. ... a pointer pretty much bypasses public/private concerns. with getlinks() the user can do what they will to the memory, there is no protection. if that bothers you, you can return a copy, or use a vector, or try to force a const on it (this is still bypassable but you have to do it actively rather than passively/by mistake).
๐ŸŒ
Esdiscuss
esdiscuss.org โ€บ topic โ€บ array-pointer-getter
Array pointer getter?
I heard about SIMD, Atomics, etc. and they accept as first arguments "array, index", so because ES6 have spreads, I suggest to make next things: let array = [0, 2, 3]; let ptr = array{0}; // don't get value, but pair of [array, 0], also this is proxied which you have setter and getter "value" let ptr = array.ptr(0); // alternate, functional ptr.value = 1; // setter let acceptance = ptr.value; // getter // also you can just use these pointers without extra indexing let idx = Atomics.add(...ptr, 1); It should available for both: typed and regular arrays.
๐ŸŒ
Weber State University
icarus.cs.weber.edu โ€บ ~dab โ€บ cs1410 โ€บ textbook โ€บ 9.Classes_And_Objects โ€บ access.html
9.9. Access Functions: Getters and Setters
They are typically small and simple, but their specialized use doesn't limit their complexity. Getters typically return data by value (i.e., by copy) to maintain encapsulation, allowing clients to use the data but not to change the object. Return by value is the default behavior for fundamental types and objects without pointer members.
๐ŸŒ
YouTube
youtube.com โ€บ watch
Getters & Setters | C++ | Tutorial 30 - YouTube
Source Code - http://www.giraffeacademy.com/programming-languages/c++/ This video is one in a series of videos where we'll be looking at programming in c++. ...
Published: November 7, 2017