Accessor function for a member array

What is the simplest way to write an accessor function for a member array.

class myClass
{
public:
...
getMyArray();

private:
int myArray[10];
};

Thanks
Last edited on
1
2
3
myClass classObject;

classObject.getMyArray();
I think you misunderstood what I was trying to say. My question is how to write an accessor function for a private member variable array, not on how to call the accessor member function.
You want the implementation of getMyArray() ???

When I have an array member, I usually provide an element accessor and a count, not access to the array directly.

Do you have a more concrete problem that you're wanting to solve?
the problem is that i need all the elements from the array, This is basically what i have,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class myClass
{
public:
...
getMyArray();
private:
...
int myArray[10];
};

int main()
{
...
// I need to access all the elements of the array here

return 0;
}
closed account (D80DSL3A)
If you need access to all of the elements at once then return a pointer from the function.
Return a constant* if you don't want the user to change the array values.
 
const int* getMyArray(){ return myArray; }


To return a single value stored in the array, pass an int argument and return an int result:
 
int getMyArray(int n){ return myArray[n]; }


EDIT: typo
Last edited on
Using (e.g.) getElemCount(), getElem() still allows you to access all elems, but would allow you to check that accessing is only done with a valid index.
Topic archived. No new replies allowed.