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

Author Topic: sf::Udpsocket NonCopyable?  (Read 2096 times)

0 Members and 1 Guest are viewing this topic.

BroManSalaam

  • Guest
sf::Udpsocket NonCopyable?
« on: February 20, 2017, 02:54:12 am »
I have a Client and a Player class

class Client {
   // socket & networking info...

    sf::UdpSocket socket;

};
 

class Player : public Client {

   //Player info...
};
 

Then I make a player object and try and push it into a std::vector...

void addPlayer(Player p) {
vector.push_back(p);
}
 

Are sf::sockets not able to be copied by default, or am I doing something wrong?

I was thinking that becuase of this...

SFML/Network/Socket.hpp:45:24: note: ‘sf::Socket::Socket(const sf::Socket&)’ is implicitly deleted because the default definition would be ill-formed: class SFML_NETWORK_API Socket : NonCopyable
                        ^
neither of my classes have any variables that would prevent them from becoming NonCopyable.
I could fix this by changing the vector from objects to pointers, but that's against my design.
Please help, thanks

« Last Edit: February 20, 2017, 06:31:27 am by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Udpsocket NonCopyable?
« Reply #1 on: February 20, 2017, 06:34:48 am »
Indeed, sockets are not copyable (what would a copied socket mean? they can't be open on the same address and port, so that would be a non-sense).

And your Player class should be non-copyable as well, unless it makes perfect sense to be able to clone Player instances -- but I doubt it.

A clean solution would be to use move semantics only, but right now SFML classes don't support that. So yes, the only way to address this issue is to use a pointer and dynamic allocation.
Laurent Gomila - SFML developer

BroManSalaam

  • Guest
Re: sf::Udpsocket NonCopyable?
« Reply #2 on: February 20, 2017, 05:55:28 pm »
Okay, thanks for clearing that up. I'll store polymorphic pointers to the players instead.

My player design was going to follow this sort of path...

 Class Client {};  //Someone connected to the network

class Spectator : Client {}; // someone spectating the game

class Player : Client {};  // a generic class for someone actively playing in the game

// example class
Knight : Player // a more specific version of a player that has specific skills/stats.

int main() {
Knight knight;
Player *player = &knight;

vector.push_back(player);
}