我真的非常感谢那些给我帮助的人.谢谢!
struct MyImage
{
BYTE* pImage;
int width;
int heigth;
};
MyImage* pMyImage = new MyImage;
pMyImage->pImage = new BYTE[width * heigth];
Run Code Online (Sandbox Code Playgroud)
我应该这样做吗?
delete [] pMyImage->pImage;
Run Code Online (Sandbox Code Playgroud)
或者我应该这样做?
delete[] pMyImage->pImage;
delete pMyImage;
Run Code Online (Sandbox Code Playgroud)
希望你的想法和谢谢.
MyImage* transform(Bitmap &gdiImage)
{
MyImage* image=new MyImage;//????MyImage
int height=gdiImage.GetHeight();
int width=gdiImage.GetWidth();
image->pImage=new BYTE[height*width];//?????????????
image->height=height;
image->width=width;
Color temp;
for(int y = 0;y < height; ++y)
for(int x = 0;x < width; ++x)
{
//????GDI+?????????????
gdiImage.GetPixel(x, y, &temp);
//???????????????????????????
*(image->pImage + y * width + x) = transformPixel(temp.GetValue());
}
return image;
}
Run Code Online (Sandbox Code Playgroud)
我的代码如下,这个函数将gdiImage转换为结构MyImage.作为一个firend说,如下,我不能新的MyImage和新的pImage,MyImage的元素.我该怎么办?谢谢
你的第二种选择是正确的,但为了避免你首先打电话delete(例如忘记这样做)错误,我建议使用适当的工具来处理这个问题.
例如,很可能你的动态分配BYTE数组可能是a的一个很好的候选者std::vector.在MyWidget可以通过某种智能指针,例如管理std::unique_ptr或std::shared_ptr等.或者你甚至可能根本不需要动态分配它,但可以在堆栈上创建它,然后在需要时传递地址.所以,也许是这样的:
// An example function dealing with images. This one draws it.
void draw(MyImage *img);
struct MyImage
{
std::vector<BYTE> image;
int width;
int heigth;
};
MyImage myImage;
myImage.image.resize(width * height);
// ...
draw(&myImage);
Run Code Online (Sandbox Code Playgroud)