是否可以存储到Class数组float组件C ++

时间:2017-11-27 12:53:23

标签: c++ arrays class pointers

我是C ++的新手,我有一个问题。我有一个float *类型的数组。我也有类Color *缓冲区。我想将数组的组件存储到缓冲区中。(或者让缓冲区的指针看到与数组相同的内存空间,但我还不熟悉C ++而且我仍然无法清楚地理解指针)这可能吗?如果没有,有什么其他方法可以使它成为可能吗?

编辑:Color类是一个包含修改PPM文件数据的所有方法的类。我需要将这些数据存储到float数组中。然后我必须修改数据。但是有一个初始化的Color *缓冲区,我无法理解如何将数据从数组移动到此Color *缓冲区中,以便将它用于此Image类的所有方法:
        班级形象     {     公共:

protected:
    Color * buffer;                              //! Holds the image data.

    unsigned int width,                          //! The width of the image (in pixels)
        height;                      //! The height of the image (in pixels)

public:
    // metric accessors

    /*! Returns the width of the image
    */
    const unsigned int getWidth() const { return width; }

    /*! Returns the height of the image
    */
    const unsigned int getHeight() const { return height; }

    // data accessors

    /*! Obtains a pointer to the internal data.
    *
    *  This is NOT a copy of the internal image data, but rather a pointer
    *  to the internally allocated space, so DO NOT attempt to delete the pointer.
    */
    Color * getRawDataPtr();

    /*! Obtains the color of the image at location (x,y).
    *
    *  The method should do any necessary bounds checking.
    *
    *  \param x is the (zero-based) horizontal index of the pixel to get.
    *  \param y is the (zero-based) vertical index of the pixel to get.
    *
    *  \return The color of the (x,y) pixel as a Color object. Returns a black (0,0,0) color in case of an out-of-bounds x,y pair.
    */
    Color getPixel(unsigned int x, unsigned int y) const;

    // data mutators

    /*! Sets the RGB values for an (x,y) pixel.
    *
    *  The method should perform any necessary bounds checking.
    *
    *  \param x is the (zero-based) horizontal index of the pixel to set.
    *  \param y is the (zero-based) vertical index of the pixel to set.
    *  \param value is the new color for the (x,y) pixel.
    */
    void setPixel(unsigned int x, unsigned int y, Color & value);

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

    // constructors and destructor

    /*! Default constructor.
    *
    * By default, the dimensions of the image should be zero and the buffer must be set to nullptr.
    */
    Image() {};

    /*! Constructor with width and height specification.
    *
    * \param width is the desired width of the new image.
    * \param height is the desired height of the new image.
    */
    Image(unsigned int width, unsigned int height);

    /*! Constructor with data initialization.
    *
    * \param width is the desired width of the new image.
    * \param height is the desired height of the new image.
    * \param data_ptr is the source of the data to copy to the internal image buffer.
    *
    * \see setData
    */
    Image(unsigned int width, unsigned int height, const Color * data_ptr);

    /*! Copy constructor.
    *
    * \param src is the source image to replicate in this object.
    */
    Image(const Image &src);

    /*! The Image destructor.
    */
    ~Image();

    /*! Copy assignment operator.
    *
    * \param right is the source image.
    */
    Image & operator = (const Image & right);

    /*!
    * Loads the image data from the specified file, if the extension of the filename matches the format string.
    *
    * Only the "ppm" extension is supported for now. The extension comparison should be case-insensitive. If the
    * Image object is initialized, its contents are wiped out before initializing it to the width, height and data
    * read from the file.
    *
    * \param filename is the string of the file to read the image data from.
    * \param format specifies the file format according to which the image data should be decoded from the file.
    * Only the "ppm" format is a valid format string for now.
    *
    * \return true if the loading completes successfully, false otherwise.
    */

    bool load(const std::string & filename, const std::string & format);

    /*!
    * Stores the image data to the specified file, if the extension of the filename matches the format string.
    *
    * Only the "ppm" extension is supported for now. The extension comparison should be case-insensitive. If the
    * Image object is not initialized, the method should return immediately with a false return value.
    *
    * \param filename is the string of the file to write the image data to.
    * \param format specifies the file format according to which the image data should be encoded to the file.
    * Only the "ppm" format is a valid format string for now.
    *
    * \return true if the save operation completes successfully, false otherwise.
    */
    bool save(const std::string & filename, const std::string & format);

};

0 个答案:

没有答案