0

How to return object from another library in c++

I'm writing a mini library for myself based on SFML so I can do things with less code. I'm doing a function that sets up a window. at the end, I want to return the object. int setup(int width, int height, std::string name){ sf::RenderWindow window(sf::VideoMode(width, height), name); return window; } It throws an error regardless to what prefix I give the function. How do I return the object? I can't setup the game loop without it. preferable I'd do window = setup(500, 500, "test"); // game loop here I've searched the q/a and none of the answers work

25th Feb 2022, 6:01 PM
MrWaalrus
MrWaalrus - avatar
1 Respuesta
+ 1
It appears that the code is declaring a local object (on the stack) and attempting to return it. It would fail because the local object goes out of scope upon function exit. You must call the constructor to create a new object (on the heap) so it will persist after the return. Also, the function type must be a pointer of the object class you are returning. There is an example here showing how to return an object created inside a function: https://stackoverflow.com/questions/26294543/cwhen-creating-a-new-objects-inside-a-function-and-returning-it-as-result-mu#26294832
25th Feb 2022, 8:32 PM
Brian
Brian - avatar