I'm trying to chain some functions but right after calling the first function, the destructor is called; then at the end of the scope the destructor is called again.
int i=0;
class Winbitmap{
public:
int index=i++;
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::Bitmap* bitmap;
Winbitmap();
~Winbitmap();
Winbitmap& getCapture(int,int,int,int);
};
Winbitmap::Winbitmap(){ Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); }
Winbitmap::~Winbitmap(){
//{delete bitmap;}
std::wcout << L"destructed:" << index << std::endl;
Gdiplus::GdiplusShutdown(gdiplusToken);
}
Winbitmap& Winbitmap::getCapture(int x=0, int y=0, int w=0, int h=0) {
bitmap = new Gdiplus::Bitmap(captureWindow(GetDC(GetDesktopWindow()),x,y,w,h),NULL);
std::wcout << L"captured:" << index << std::endl;
return *this;
}
This is how I intend to use it:
Winbitmap bitmap1 = Winbitmap().getCapture();
std::wcout<<L"confirmed1\n";
Winbitmap bitmap2 = Winbitmap().getCapture();
std::wcout << L"confirmed2\n";
//If I try to use any of the bitmaps later, the program hangs
Output:
captured:0
destructed:0
confirmed1
captured:1
destructed:1
confirmed2
destructed:1
destructed:0
How can I return correctly a reference to the object without calling the destructor?