0

I am new to Object oriented programming. I was going through some code to learn some object oriented programming. Game from Scratch C++ has some code for a game called Pang that helps learn OOP concepts. In the below code I can see that an object from the sf::RenderWindow class is created and this object is defined as static in another class. I am confused as to what is going on here and is it possible to do something like this. If someone with good familiarity of SFML could answer this I would appreciate it. Also, what does sf? stand for over here?

#pragma once
#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"
class Game
{

public:
  static void Start();

private:
  static sf::RenderWindow _mainWindow;
};
DarkPhantom
  • 155
  • 4
  • 9
  • 3
    SFML assumes that you are already fluent with C++. Please don't learn SFML to learn C++. If you want to learn C++, you should get a dedicated [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – alter_igel May 09 '19 at 23:07
  • 1
    Does your book actually recommend `static sf::RenderWindow`? That's bad. It will likely cause crashes for many users. https://en.sfml-dev.org/forums/index.php?topic=12885.0 – alter_igel May 09 '19 at 23:10
  • https://www.gamefromscratch.com/page/Game-From-Scratch-CPP-Edition-Part-2.aspx – Rejoy Mathews May 09 '19 at 23:11
  • I have some familiarity with the concepts. I am trying to go through a project that is already developed to test my understanding of the concepts. – Rejoy Mathews May 09 '19 at 23:12
  • Would you recommend any project that is out there (and conceptually correct) and will help me strengthen my Object Oriented Concepts – Rejoy Mathews May 09 '19 at 23:14

1 Answers1

2

sf is the namespace, similar to std being the namespace for cout. Technically it would mean "Simple and Fast", but really has no importance other than to provide a unique context to define functions in. This is so that, for example, you could have a printNumber() function in both foo and bar namespaces, with distinct implementations, and you could call each of them with foo::printNumber() and bar::printNumber(). It's an organisation technique.

In this context, a static _mainWindow member means there is only one instance created no matter how many Game class instances you create there will only ever be one _mainWindow. Because of this, you won't access it how you normally would, this->mainWindow, but because the instance is independent of any particular Game instance, you access it with Game::_mainWindow. Not sure This is probably just to ensure only one single window is ever open.

Note: Both namespace and static use the syntax foo::bar which means "look for bar in context foo".

jackw11111
  • 1,457
  • 1
  • 17
  • 34
  • 2
    The explanation of `static` here feels a bit weird. `static` is a storage duration, and is pretty unrelated to type safety. `static` also doesn't "make use of" the scope resolution operator. A class simply defines a scope, and that is not changed by `static` in any way. Also, instead of `this.mainWindow`, the correct syntax would be `this->_mainWindow`, although simply `_mainWindow` will work inside any member function. – alter_igel May 10 '19 at 00:07
  • Storage duration (i.e. lifetime and location of an object) and types (i.e primitive, class, pointer, reference, etc) are orthogonal, and can be used in basically any combination. Also, the SRO `::` cannot be overloaded. Are you looking for [name lookup](https://en.cppreference.com/w/cpp/language/lookup)? – alter_igel May 10 '19 at 00:27