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

Author Topic: SoundSource and the rule of 3  (Read 3553 times)

0 Members and 2 Guests are viewing this topic.

deadalnix

  • Newbie
  • *
  • Posts: 45
    • View Profile
SoundSource and the rule of 3
« on: November 28, 2011, 12:52:17 pm »
Soundsource define a copy constructor but doesn't define a assignment operator. I don't really see the point of that.

As the main constructor is protected and the destructor virtual, SoundSource isn't made to exist in itself, but to be used as a base class. So the copy constructor shouldn't be defined.

Or does cases exists where it can be used by itself ? In such a case, the copy constructor made sense, but overloading assignment should be done too.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SoundSource and the rule of 3
« Reply #1 on: November 28, 2011, 01:03:21 pm »
The copy constructor is called by the copy constructor of derived classes, so it makes sense even if the SoundSource class itself will never be instanciated and used publicly.

There's no assignment operator because there's nothing to assign (the internal sound handle is tied to its SoundSource instance and must not be modified on assignment).

But I agree that this is an implementation detail, the class should provide something, even empty, so that derived class can implement their own assignment operator without asking too much questions :)

However, the base class should not provide an operator= but rather a swap() function. See the "copy and swap" implementation of assignment operators for more details.
Laurent Gomila - SFML developer

deadalnix

  • Newbie
  • *
  • Posts: 45
    • View Profile
SoundSource and the rule of 3
« Reply #2 on: November 28, 2011, 04:40:46 pm »
So maybe the assignment operator should be declared private and never defined to avoid any mistakes ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
SoundSource and the rule of 3
« Reply #3 on: November 28, 2011, 04:43:59 pm »
I think it would be misleading, compared to the solution of providing a swap() function. A private assignment operator usually means that copy semantics are disabled for the class.

But... who would make any mistake? I'm the only one to use this stuff ;)
Laurent Gomila - SFML developer

deadalnix

  • Newbie
  • *
  • Posts: 45
    • View Profile
SoundSource and the rule of 3
« Reply #4 on: December 02, 2011, 10:01:19 pm »
Well, I'm experienced enough to know that I do mistake and stupid stuff sometime (see the thread in graphic for example). And at this point, I never met anybody that wasn't making some of them :D

Coding is a practice that show us how fallible we are, and how defensive we should be against ourselves. Sure, SoundSource is not meant to be used directly, but subclassed. Sure also it is in headers, so anybody can use it, whether it makes sense or not. So, by prevention, I think it should be done.

 

anything