Reputation: 45
Can anyone tell me how I can use object that I defined in main like this
int main()
{
sf::RenderWindow window;
}
And now I want to use window
object in class that I made. But it must point out to the same main
's window
. How can we use it? Can anyone explain it with some code example?
I am using SFML library of C++.
Upvotes: 0
Views: 95
Reputation: 726969
There are several solutions to making objects created in main
available to other code:
window
as a parameter, by pointer or by reference, to code that needs to use itwindow
, use that object throughout your code, and set its pointer in main
before calling any other codemain
. This is not a good recommendation.Upvotes: 1
Reputation: 50026
You can pass them as references or pointers, ex.:
class CEngine {
sf::RenderWindow& window;
public:
CEngine(sf::RenderWindow& wnd) : window(wnd) {}
// ...
};
int main()
{
sf::RenderWindow window;
CEngine engine(window);
}
Upvotes: 2