如何使用C ++在结构中的堆内存中定义变量?

时间:2016-12-14 17:10:45

标签: c++

我有2个结构,第一个颜色:

struct Color
{
    unsigned char red;
    unsigned char green;
    unsigned char blue;
};

图像结构:

struct matrixImage
{
    Image Img;
    struct Color T[MAX][MAX];
    int Width;
    int Height;
};

T表中,我正在存储图像的像素。但是阵列的最大尺寸是低(堆叠),所以我不能存储图像的每个像素。我知道如何在Heap中定义一个像这样的数组

- > struct Color *newTable = new struct Color[anyNumber];

但我怎么能在struct matrixImage中写这个,有什么帮助吗?

4 个答案:

答案 0 :(得分:4)

我做这样的事情:

//Switched to a class because we need some encapsulation or this will be bug-prone.
class matrixImage
{
    //Do you need this? What kind of object is "Image"?
    //Image Img;
    std::vector<Color> pixel_data;
    int Width;
    int Height;
public:
    matrixImage(int width = 1, int height = 1) :
    Width(width), Height(height), pixel_data(width * height) 
    {
    }

    //You can add bounds-checking if you need it, i.e. make sure y is less than Height, 
    //make sure x is less than Width. Not every application needs it, and you need a 
    //clear semantic of "what should happen" if you specify an invalid index.
    Color & get_color(int x, int y) {
        return pixel_data[y * Width + x];
    }
    //We have two versions to handle the case where your object is made "const"
    Color const& get_color(int x, int y) const{
        return pixel_data[y * Width + x];
    }

    //Hey, we can use the function we just defined above this one!
    void set_color(int x, int y, Color c) {
        get_color(x, y) = c;
    }

    //This only resizes the canvas. Doing proper "Resizing" is beyond the scope of what 
    //we're discussing here.
    void set_size(int new_width, int new_height) {
        std::vector<Color> new_data(new_width * new_height);

        //This case is pretty easy
        if(new_width == Width) {
            std::copy(
                pixel_data.begin(), 
                pixel_data.begin() + std::min(pixel_data.size(), new_data.size()), 
                new_data.begin()
            );
        //This gets complicated
        } else if(new_width < Width) {
            for(size_t y = 0; y < std::min(Height, new_height); y++) {
                std::copy(
                    pixel_data.begin() + y * Width,
                    pixel_data.begin() + y * Width + new_width,
                    new_data.begin() + y * new_width
                );
            }
        //Similar logic, but slightly different.
        } else {
            for(size_t y = 0; y < std::min(Height, new_height); y++) {
                std::copy(
                    pixel_data.begin() + y * Width,
                    pixel_data.begin() + (y + 1) * Width,
                    new_data.begin() + y * new_width
                );
            }
        }
        pixel_data = new_data;
        Width = new_width;
        Height = new_height;
    }

    //I leave this last one as an exercise to the reader, as it's beyond my expertise.
    void resize(int new_width, int new_height);
};

这将为您处理动态内存分配(当然还有解除分配),并为您提供一些基本功能,以便在您绝对需要时直接使用基础数据。

答案 1 :(得分:3)

你有自己的问题的答案,即在结构中使用指针。然后,您将使用new获取堆内存并将结果存储在指针中。

但不,不要手动执行此操作。学习如何写&#34;现代C ++&#34; (查看)几乎从不使用newdelete,并且有充分的理由。

在此处使用std::vector。真。

理想情况下,您可以将整个事物包装到某种Bitmap类中,该类隐藏向量并提供成员函数来访问像素。不是虚函数,因为效率对图形非常重要。

答案 2 :(得分:0)

#include <memory>

struct matrixImage
{
    Image Img;
    int Width;
    int Height;
    std::unique_ptr<Color[]> colors;
    Color& color(int x, int y);
};

Color& matrixImage::color(int x, int y)
{
    if (x >= Width || y >= Height)
        throw std::out_of_range("bad co-ordinates");
    return colors[y*Width + x];
}

// ...

matrixImage mimg;
mimg.Width = 20;
mimg.Height = 100;
mimg.colors = std::make_unique<Color>(mimg.Width*mimg.Height);

最好将分配封装在构造函数中,将宽度和高度作为参数:

matrixImage::matrixImage(Image img, int width, int height)
: Img(img), Width(width), Height(height), colors(std::make_unique<Color>(mimg.Width*mimg.Height))
{ }

答案 3 :(得分:-4)

概括地说,使用int而不是你的枚举类型,使用构造函数来执行此操作将如下所示:

struct A {
     int x;
     int * p;
     A() : x(0), p( new int[100] ) {}
};

您还需要一个析构函数和复制构造函数。