Reputation: 1
How to create a dynamic bitmap array?
I tried it like this:
Bitmap* bit = new int(5);
But it didn't work out.
After, I tried like this:
Bitmap* bit = (Bitmap*)malloc(6);
bit[5] = new Bitmap(sFileName);
But it didn't work, too.
EDIT
In .h, I have:
std::shared_ptr<CachedBitmap> *CachedBitmap;
In .cpp (WM_PAINT):
Bitmap* bit = new Bitmap(L"file");
Graphics graphics(hdc);
(CachedBitmap[0]) = PNGLoader::createCachedBitmap(bit, hdc);
graphics.DrawCachedBitmap(CachedBitmap[0].get(), position.x, position.y);
std::shared_ptr<CachedBitmap> PNGLoader::createCachedBitmap(Bitmap* originalBitmap, HDC hdc)
{
Graphics graphics(hdc);
return std::make_shared<CachedBitmap>(originalBitmap, &graphics);
}
When I run the program, I have an exception error because CachedBitmap[0]
is nullptr.
I tried:
(**CachedBitmap) = std::make_shared<::CachedBitmap>((new ::CachedBitmap(bit, &graphics))[5]);
(*CachedBitmap[0]) = PNGLoader::createCachedBitmap(bit, hdc);
graphics.DrawCachedBitmap(CachedBitmap[0]->get(), position.x, position.y);
But I have error:
Gdiplus::CachedBitmap::CachedBitmap: unable to access private member declared in "Gdiplus" class::CachedBitmap"
Upvotes: 0
Views: 525
Reputation: 596166
Bitmap* bit = new int(5);
This doesn't work because you are dynamically allocating a single int
whose value is 5
, and then trying to assign the address of that int
to a Bitmap*
pointer. Obviously, an int
is not a Bitmap
, so the compiler does not allow this assignment.
You would need this instead:
Bitmap* bit = new Bitmap[5];
However, that doesn't work either, because GDI+'s Bitmap
class does not have a default constructor. So, you need an array of Bitmap*
pointers, not an array of Bitmap
objects. Which leads us to the next example...
Bitmap* bit = (Bitmap*)malloc(6); bit[5] = new Bitmap(sFileName);
This doesn't work because you are allocating only 6 bytes total, but you would need to allocate enough bytes for 6 Bitmap*
pointers in order for bit[5]
to hold a valid Bitmap*
pointer, eg:
Bitmap** bit = (Bitmap**) malloc(sizeof(Bitmap*) * 6);
bit[0] = new Bitmap(sFileName);
bit[1] = ...;
...
bit[5] = ...;
But, don't use malloc()
in C++, use new[]
instead:
Bitmap** bit = new Bitmap*[6];
bit[0] = new Bitmap(sFileName);
bit[1] = ...;
...
bit[5] = ...;
And don't forget to delete
whatever you new
when you are done using it:
delete bit[0];
delete bit[1];
...
delete bit[5];
delete[] bit;
But, do you really need the array itself to be allocated dynamically?
Bitmap* bit[6];
...
bit[0] = new Bitmap(sFileName);
bit[1] = ...;
...
bit[5] = ...;
...
delete bit[0];
delete bit[1];
...
delete bit[5];
However, in modern C++ coding, avoid new
/delete
manually, use standard containers and smart pointers to handle all of the memory management for you, eg:
std::vector<std::unique_ptr<Bitmap>> bit;
...
bit.resize(5);
bit[0] = std::make_unique<Bitmap>(sFileName);
bit[1] = ...;
...
Or:
std::unique_ptr<Bitmap> bit[5];
...
bit[0] = std::make_unique<Bitmap>(sFileName);
bit[1] = ...;
...
UPDATE:
std::shared_ptr<CachedBitmap> *CachedBitmap;
There is no good reason to have a pointer to a smart pointer. Drop the *
from the declaration:
std::shared_ptr<CachedBitmap> CachedBitmap;
...
Bitmap bit(L"file");
Graphics graphics(hdc);
CachedBitmap = PNGLoader::createCachedBitmap(&bit, hdc);
graphics.DrawCachedBitmap(CachedBitmap.get(), position.x, position.y);
Or, if you really need an array of smart pointers:
std::vector<std::shared_ptr<CachedBitmap>> CachedBitmap;
...
CachedBitmap.resize(5);
Bitmap bit(L"file");
Graphics graphics(hdc);
CachedBitmap[0] = PNGLoader::createCachedBitmap(&bit, hdc);
graphics.DrawCachedBitmap(CachedBitmap[0].get(), position.x, position.y);
Or:
std::shared_ptr<CachedBitmap> CachedBitmap[5];
...
Bitmap bit(L"file");
Graphics graphics(hdc);
CachedBitmap[0] = PNGLoader::createCachedBitmap(&bit, hdc);
graphics.DrawCachedBitmap(CachedBitmap[0].get(), position.x, position.y);
Upvotes: 1