-5

This is probably a dumb question but since i am new to programming and especially c++ i figured i would ask here.I have the following method inside a class:

/*! Copies the image data from an external raw buffer to
 *  the internal image buffer.
 *
 *  The member function ASSUMES that the input buffer is of a size compatible
 *  with the internal storage of the Image object and that the data buffer has
 *  been already allocated. If the image buffer is not allocated or the
 *  width or height of the image are 0, the method should exit immediately.
 *
 *  \param data_ptr is the reference to the preallocated buffer from where to
 *  copy the data to the Image object.
 */
void setData(const Color * & data_ptr);

Does the * & mean anything special? I know Color* is obviously a pointer but im having trouble with this. The external raw buffer mentioned in the comments of the code is a float array and the internal image buffer is a Color* buffer(Color is another class inside the same namespace).

Edit:Wow thanks for all the downvotes! Its not like i mentioned im at a beginner level and just now starting to get into the language. Got to love the stackoverflow community. You make learning so fun!

  • 1
    `&` denotes a reference. `const Color * & data_ptr` means `data_ptr` is a reference to a `const Color *`. See https://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value – François Andrieux Nov 28 '17 at 20:51
  • 1
    Possible duplicate of [How do I use Reference Parameters in C++?](https://stackoverflow.com/questions/2564873/how-do-i-use-reference-parameters-in-c) – frslm Nov 28 '17 at 20:51
  • 1
    `Color*` is a pointer, and `&` in type definition means that it's a reference to a specified type. Hence - it's a reference to a pointer to a `Color`. Please read a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Nov 28 '17 at 20:52

1 Answers1

4

Read right to left: a reference to a pointer to Color.

That is, you are passing the pointer by reference, so that the function can change it. The const applies to Color, not to the pointer or the reference. I prefer writing the same as Color const* & data_ptr, so that you can read right-to-left and see what the const applies to.

Cris Luengo
  • 55,762
  • 10
  • 62
  • 120