This is a continuation of a previous discussion here... I'm trying to convert an existing GDI+ practice program to use for the various GDI+ objects...
So the existing (working) code is this:
Bitmap *pbitmap = new Bitmap(L"images.png");
nWidth = pbitmap->GetWidth();
nHeight = pbitmap->GetHeight();
Based on input in a recent thread here, I tried this: std::vector<Bitmap*> pbitmap = new Bitmap(L"images.png");
Okay, I already know this is not correct; I don't need to use 'new' with vector, but that immediately poses the question: how do I input the filename into the vector??
I tried:
std::vector<Bitmap*> pbitmap ;
pbitmap.push_back(L"images.png"); // HAH!! This doesn't even *begin* to look
// sensible, and indeed, it is not
nWidth = pbitmap->GetWidth();
nHeight = pbitmap->GetHeight();
and neither of these assignments are valid either:
example1.cpp:146:21: error: base operand of '->' has non-pointer type 'std::vector<Gdiplus::Bitmap*>'
146 | nWidth = pbitmap->GetWidth();
| ^~
example1.cpp:147:21: error: base operand of '->' has non-pointer type 'std::vector<Gdiplus::Bitmap*>'
147 | nHeight = pbitmap->GetHeight();
I spent a fair bit of time searching the web for pages discussing using with gdiplus, but I don't see anything even remotely useful... So please enlighten me here...
Just for background on me, I have almost 40 years experience with C, mostly in embedded environments, but although I've been using C++ for a decade or so now, it has mostly been "C++ as a better C", as they used to say in Dr Dobbs Journal... it is only recently that I'm trying to actually adopt the correct visions...
vectoris not aBitmap.operator new/delete, but at the same time doesn't have virtual destructors in its base classes (thus breaking the usual polymorphic usage). However, perhaps trystd::vector<std::unique_ptr<Gdiplus::Bitmap>> my_vec;— this creates an (initially empty) vector of smart pointers toBitmap-objects. And thenmy_vec.push_back(std::make_unique<Gdiplus::Bitmap>(L"images.png"));nWidth = my_vec[0]->GetWidth(); nHeight = my_vec[0]->GetHeight();.) By using a smart pointer, you don't need to deal with manual memory management: the Bitmap object will be automatically deleted when the smart poiner will get destroyed, i.e., either by manually removing it from the vector by doingmy_vec.pop_back()or just when the vector itself will naturally "go out of scope" (and thus automatically destroy all of its elements).pbitmap.push_back(L"images.png");- that makes no sense. You have a container ofBitmap*(pointers to Bitmap) and you are trying to put a string into that container. I'm curious as to what led you to think that that makes sense..?