如何从一个函数返回一个GDI位图:: [英] How to return a GDI::Bitmap from a function
问题描述
我如何从一个函数返回一个位图?
这code不起作用:编译错误
How can I return a bitmap from a function? This code doesn't work: Compiler error
Gdiplus::Bitmap create()
{
Gdiplus::Bitmap bitmap(10,10,PixelFormat32bppRGB);
// fill image
return bitmap;
}
我不想返回一个指针然后生成内存泄漏的机会。 (或者,如果有办法避免内存泄漏的这种机会)
I don't want to return a pointer as then it generate a chance for memory leak. (or if there is way to avoid this chance of memory leak)
推荐答案
当你从一个函数返回一个对象,编译器需要产生code以复制或(在C ++ 11)移动的对象。
When you return an object from a function, the compiler needs to generate code to either copy or (in C++11) move that object.
Gdiplus ::位图
不具有可访问的拷贝构造函数(和predates C ++ 11,所以没有任何移动的),所以你不能做这个。取决于你如何使用它,你可能会考虑使用类似的的std ::的unique_ptr
或可能是的std :: shared_ptr的
代替。
Gdiplus::Bitmap
does not have an accessible copy constructor (and predates C++11, so no moving either) so you're not allowed to do this. Depending on how you're using it, you might consider using something like an std::unique_ptr
or possibly an std::shared_ptr
instead.
另外,你可能希望创建位图
父,并简单地传递一个指针或引用它有你的函数填写它作为必要的。
Alternatively, you might want to create the Bitmap
in the parent, and simply pass a pointer or reference to it to have your function fill it in as needed.
这篇关于如何从一个函数返回一个GDI位图::的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!