Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Init of static member object  (Read 5329 times)

0 Members and 2 Guests are viewing this topic.

mpaw

  • Newbie
  • *
  • Posts: 5
    • View Profile
Init of static member object
« on: May 24, 2020, 04:42:14 pm »
Hi.

I have class in which I have static member, which is RenderWindow object. How can I initialize it?

class A
{
  public:
  static sf::RenderWindow window;
};

How Can I init it in my program?
Thanks

Athenian Hoplite

  • Newbie
  • *
  • Posts: 19
  • Nenikikamen !
    • View Profile
Re: Init of static member object
« Reply #1 on: May 24, 2020, 05:12:54 pm »
Like all other static objects. In your cpp file I'd imagine. 

// Header file
class A
{
  public:
  static sf::RenderWindow window;
};

// Cpp file
sf::RenderWindow A::window;
 

You can use the default constructor and then at runtime use the sf::RenderWindow::create method to (re) create the window with the parameters you somehow got programmatically or replace "= sf::RenderWindow();" with the usual constructor and pass the arguments you want hardcoded.
« Last Edit: May 24, 2020, 07:39:54 pm by Athenian Hoplite »
"By will of the Athenian People be it resolved:
If anyone rises up against the people for tyranny or join in establishing the tyranny or overthrow the People of the Athenians and the democracy in Athens, whoever kills him who does these things shall be blameless." - Athenian Law 337 BC

mpaw

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Init of static member object
« Reply #2 on: May 24, 2020, 05:33:25 pm »
I tried, but there is no static constructor defined for RenderWindow :(

Athenian Hoplite

  • Newbie
  • *
  • Posts: 19
  • Nenikikamen !
    • View Profile
Re: Init of static member object
« Reply #3 on: May 24, 2020, 05:42:59 pm »
What I told you before works. sf::RenderWindow defines more than one constructor which can be used to instantiate static variables the way I described. You can also derive a class from sf::RenderWindow if you prefer but that is beside the point.

Note: C++ doesn't have "static constructors" like in C#.

This works:
class A
{
  public:
  static sf::RenderWindow window;
};

sf::RenderWindow A::window;
 
« Last Edit: May 24, 2020, 07:40:03 pm by Athenian Hoplite »
"By will of the Athenian People be it resolved:
If anyone rises up against the people for tyranny or join in establishing the tyranny or overthrow the People of the Athenians and the democracy in Athens, whoever kills him who does these things shall be blameless." - Athenian Law 337 BC

mpaw

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Init of static member object
« Reply #4 on: May 24, 2020, 05:56:10 pm »
I get error:

... error: use of deleted function 'sf::RenderWindow::RenderWindow(const sf::RenderWindow&)'|
... note: 'sf::RenderWindow::RenderWindow(const sf::RenderWindow&)' is implicitly deleted because the default definition would be ill-formed:|

Athenian Hoplite

  • Newbie
  • *
  • Posts: 19
  • Nenikikamen !
    • View Profile
Re: Init of static member object
« Reply #5 on: May 24, 2020, 06:05:18 pm »
What code is producing that error ? I have compiled the code with no errors so this is not what is giving you that.
"By will of the Athenian People be it resolved:
If anyone rises up against the people for tyranny or join in establishing the tyranny or overthrow the People of the Athenians and the democracy in Athens, whoever kills him who does these things shall be blameless." - Athenian Law 337 BC

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Init of static member object
« Reply #6 on: May 24, 2020, 07:18:32 pm »
Just

sf::RenderWindow A::window;

No need to copy-construct from a default-constructed instance ;)
Laurent Gomila - SFML developer

Athenian Hoplite

  • Newbie
  • *
  • Posts: 19
  • Nenikikamen !
    • View Profile
Re: Init of static member object
« Reply #7 on: May 24, 2020, 07:39:43 pm »
Just

sf::RenderWindow A::window;

No need to copy-construct from a default-constructed instance ;)
whoops-a-daisy ;D ;D
edited
"By will of the Athenian People be it resolved:
If anyone rises up against the people for tyranny or join in establishing the tyranny or overthrow the People of the Athenians and the democracy in Athens, whoever kills him who does these things shall be blameless." - Athenian Law 337 BC

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Init of static member object
« Reply #8 on: May 26, 2020, 11:59:22 am »
Beyond that, you should not use static variables for SFML resources, as the initialization and destruction order of static/global variables is undefined and can lead to crashes.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/