【问题标题】:what does the *& mean in :void setData(const Color * & data_ptr);:void setData(const Color * & data_ptr); 中的 *& 是什么意思
【发布时间】:2018-05-12 10:45:37
【问题描述】:

这可能是一个愚蠢的问题,但由于我是编程新手,尤其是 c++,我想我会在这里问。我在一个类中有以下方法:

/*! 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);

* & 有什么特别的含义吗?我知道 Color* 显然是一个指针,但我遇到了麻烦。代码的cmets中提到的外部原始缓冲区是一个浮点数组,内部图像缓冲区是一个Color*缓冲区(Color是同一命名空间内的另一个类)。

编辑:哇,感谢所有反对票!它不像我提到的我是初学者,现在才开始接触这门语言。必须爱上 stackoverflow 社区。你让学习变得如此有趣!

【问题讨论】:

标签: c++ methods colors ppm


【解决方案1】:

从右到左阅读:对指向 Color 的指针的引用。

也就是说,您通过引用传递指针,以便函数可以更改它。 const 适用于颜色,而不适用于指针或引用。我更喜欢写和Color const* & data_ptr一样的写法,这样你就可以从右到左阅读,看看const适用于什么。

【讨论】:

    猜你喜欢
    • 2011-11-18
    • 1970-01-01
    • 2020-05-01
    • 2013-01-20
    • 2013-03-09
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多