C ++ Encapsulating Arrays

Sorry if this was asked before, but I cannot find a clear enough answer.

What is the correct way to provide read-only public access to an array element in C ++?

if I have a class like the following:

   class Particle { double _position[10]; public: double* get_position() { return _position; } }; 

It seems to me very bad to return a pointer to an array, since this means that it can be changed to at any time outside the class, is it enough to return a const pointer?

I saw other questions about using arrays in C ++, and how best to use vectors, however I am very interested in this problem.

As you can see, I'm just a C ++ noob, sorry if this is a stupid question.

PD Sorry for my bad english.

+4
source share
6 answers

You can return a const pointer:

 class Particle { double _position[10]; public: const double* get_position() const { return _position; } }; 

Note that I also created a const member function (second const ) to tell the compiler that this member function can be called on instances of const Particle .

Note: as you already mentioned, the best solution is to use STL vectors, etc.

+4
source

Returning const * is not much better, as the caller can simply discard the constant.

Consider providing a constant iterator instead of returning a pointer. This gives users your class access to line items without revealing implementation details. If you later want to switch from a fixed-length array to an STL container, you can do this without affecting users of this class.

+7
source

Do not use a raw array; always prefer std::array<> (or boost::array<> if your compiler is not popular enough for delivery with an implementation of std:: or std::tr1:: :):

 class Particle { public: typedef std::array<double, 10> positions_t; Particle() : positions_() { } positions_t& get_positions() { return positions_; } positions_t const& get_positions() const { return positions_; } private: positions_t positions_; }; 
+3
source

Yes, return the const pointer to the array (or array element).

 const double* get_position() const { return _position; } 

FWIW, I once applied a read-only status code in a class using a const reference:

 class Foo { public: const int & err = &errcode; private: int errcode; ... } 

(Sorry if the syntax is not entirely correct.)
This allows the Foo client to read, but not modify, Foo::err , which is a constant reference to the actual private error code Foo::errcode , which is modified by member functions of the class.

0
source

Like any other member that you want to grant access to, you can return a const link to it:

 const double ( &get_position() const )[10] { return position; } 

Of course, you can use typedef to make it more readable.

Or, since it is an array, you can implement operator[] :

 const double& operator[](std::size_t n) const { return position[n]; } 
0
source

There are at least three questions / comments:

  • Array return
  • Return item
  • Return std::vector

Array return

You can always return a pointer to an array, but when transferring arrays, you always need to specify the capacity. Unfortunately, functions can only return one value, so you have to return "outside" the parameters from the function:

 double * get_position(unsigned int& capacity); // Or void get_position(double *& array_pointer, unsigned int& capacity); 

Return item

You can hide the internal representation of {array} by providing a function to return a single element. This gives the advantage of checking borders.

  double get_one_position(unsigned int index) { double result = 0.0; if (index >= MAX_CAPACITY) { // Errror handling } else { result = _position[index]; } return result; } 

Return std::vector

In C ++, we prefer std::vector array. Your problem is one of the reasons for switching. std::vector can be passed and returned from a method. In addition, std::vector supports its own capacity variables, so there is no need to pass throughput along with the array pointer.

  std::vector<double> _position(10); std::vector<double> get_positions(void) { return _position; } 

Summary

I suggest you first try to hide the implementation of the array from clients or users. If this is not possible, prefer to use std::vector for the array. Finally, use an array.

When you return the array always accompanies its variable capacity.

0
source

Source: https://habr.com/ru/post/1347283/


All Articles