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

Author Topic: sf::Time constructor  (Read 4423 times)

0 Members and 1 Guest are viewing this topic.

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
sf::Time constructor
« on: January 15, 2013, 05:50:04 pm »
Hello.
The other day I was writing a class that has a sf::Time as a member variable. That was when I noticed that the time class doesn't have a contructor other than the default one. That means I can't initilize it dirctly in the initialization list of my constructor. So I was wondering is there a specific reason why the time class doesn't have a constructor other than the default one? Or could there maybe be a constructor added that takes a duration in say milliseconds?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::Time constructor
« Reply #1 on: January 15, 2013, 06:06:04 pm »
Copy constructor and assignment operaters are created implicitly if they aren't defined explicitly (see here). The sf::Time class has a default copy constructor (aka compiler-generated copy constructor or implicitly generated copy constructor) and thus you can initialize it with a sf::Time object.

You could've also just tested it, it works fine. ;)
#include <SFML/System.hpp>

class TR
{
public:
    TR() :
        m_tr(sf::milliseconds(200)) // Init with 200 ms
    {
    }
private:
    sf::Time m_tr;
};

int main()
{
    TR tr;
}
 
« Last Edit: January 15, 2013, 09:59:11 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Time constructor
« Reply #2 on: January 15, 2013, 06:58:00 pm »
You only need to declare copy constructor, copy assignment operator and destructor if they are necessary for correct semantics. Otherwise, the compiler generates them. See also the Rule Of Three.

I personally define the Big Three almost never, and consider programs that implement them all the time badly encapsulated. By using classes that provide copy and RAII semantics, implicitly generated member functions are mostly sufficient. For example, Thor has only two classes which have an explicitly defined copy constructor.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Foaly

  • Sr. Member
  • ****
  • Posts: 453
    • View Profile
Re: sf::Time constructor
« Reply #3 on: January 15, 2013, 09:43:42 pm »
Well that's embarrassing...
Of course the default copy constructor did the trick perfectly! Just forget I asked this question.
Note to myself: I need to stop coding/posting sleep deprived...

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Time constructor
« Reply #4 on: January 15, 2013, 09:56:39 pm »
"Default copy constructor" is an unfortunate term that eXpl0it3r used. It can easily be confused with the default constructor. Call it "compiler-generated" or "implicitly generated" ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: sf::Time constructor
« Reply #5 on: January 15, 2013, 09:58:46 pm »
"Default copy constructor" is an unfortunate term that eXpl0it3r used. It can easily be confused with the default constructor. Call it "compiler-generated" or "implicitly generated" ;)
*Fixed* :P
Then again you can still miss the 'copy' part...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything