views:

172

answers:

5

hi, i've a class and there is an array set as private, how do i make the get_array() function i mean how do i return that array knowing that i will have to return a pointer of arr[0] as we know , but isn't this breaking the private rule ? is there other way of returning array ?

I actually thought of having array2 in the main , then edit that array using the class function. i think it will be a plan B if the above didn't work.

+1  A: 

You cannot return an array from a function. You can, as you say, return a pointer to the first element of an array, but this has the downside that you have to have some separate functionality to obtain the size of the array from outside of the class.

A better option would be to return a struct that contains an array. The easiest way to do this would be to use the array class, which can be found in Boost and in many standard library implementations.

For example,

class C
{
public:
    typedef boost::array<int, 5> ArrayType;
    ArrayType get_array() const { return array_; }

private:
    ArrayType array_;
};
James McNellis
yes i didn't use boost out of simplicity in my code and i didn't want it to depend on other libs.
ismail marmoush
@ismail: It's trivial to write your own boost::array, and with a relatively up-to-date compiler it's part of std. Don't use the in-built arrays unless you absolutely must.
DeadMG
@ismail: The Boost.Array header is pretty much self contained, so you should be able to pull it out of Boost and use it independently relatively easily. Or, you can easily rewrite it; it's quite a simple class.
James McNellis
You could also forward some iterators to provide access instead of returning the entire array.
mocj
ismail marmoush: then use std::vector
Frank
A: 

I would use std::vectors instead of arrays in C++. You can return them just like a primitive (vector<whatever> getter(){ return foo;}), and they tend to be easier to deal with.

anjruu
@anjruu: which one do you think better in perfomance normal array or vector ? p.s (i'm using fixed arrays so i won't need the scalability option)
ismail marmoush
Vectors are slightly more expensive than plain auto arrays. The difference being a single memory allocation and extra dereference in access --if you provide the vector size during construction or resize to the appropriate size. Now, returning a vector by value can have a great impact in performance as it will *copy* all the objects. In the general case you would return a constant reference to the internal vector. Then again, that will leak as much of your internal state as providing a reference to the array in the first place.
David Rodríguez - dribeas
If you're using fixed-length arrays, I agree with David Rodriguez, it's better to use arrays than vectors.
anjruu
As others have said, if you're using fixed-length arrays, I'd say go with `boost::array`, if that's an option (otherwise, it's pretty easy to write it yourself).
Pedro d'Aquino
choosing between array vs. vector for performance reasons without profiling and specific requirementments sounds a lot like premature optimization...
Frank
Maybe, but you have to make the choice, right, so if they are equal in all the other respects, might as well as have some rational for the decision.
anjruu
A: 

Return a const reference to the array to make it read-only.

const ArrayType& ma = myClass->get_array();

Unless your clients are evil, they should not be able to modify the array. :-) For clarity, the class with the array and the array itself cannot go out of scope while you are using it.

Edited for typo.

JustBoo
+1  A: 

From a pure OO perspective, it really depends who is going to use the private variable of your class. If it is a friend class, or another trusted entity, there is no problem in handing out the private data member.

Chubsdad
+1  A: 

You can do a few things.

The simplest is to return a pointer to the first element that is const: const int* get_array(void) const; You might want to make a function to query the size as well.

A better option might be to return a const-reference to the array. Use a typedef to make it simple:

typedef int array_type[10];
const array_type& get_array(void) const;

Another option is to wrap the array up, like boost::array. You can copy and paste Boost's header if you want, into your own array.hpp:

typedef boost::array<int, 10> array_type;
const array_type& get_array(void) const;
GMan