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

Author Topic: [SOLVED] Sorry for the stupid question, but how do I store sockets?  (Read 3316 times)

0 Members and 1 Guest are viewing this topic.

Alidaco

  • Newbie
  • *
  • Posts: 5
    • View Profile
Like the title says, I'm 100% sure that this is a stupid question, but I have been unable to figure it out for some reason. How can I store the sf::TcpSocket? The Tutorial mentions that the selector doesn't store it, so I should look into using a vector, but I can't put it into a vector because it's non-copyable. I can't just make one and leave it alone, because it would go out of scope. Do I have to manage it manually with "new" and "delete" and my own data types? Surely there's a better way, right? What am I missing here? Thanks guys, I look forward to your answers.
« Last Edit: July 22, 2013, 03:40:30 pm by Alidaco »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sorry for the stupid question, but how do I store sockets?
« Reply #1 on: July 22, 2013, 01:50:59 pm »
This is not a stupid question ;)

There are two ways to store socket instances:

1. by using pointers (wrapped into smart pointer classes!)

2. by using move semantics, if you have a C++11 compiler and... when SFML supports it ;D (probably in SFML 2.2)
Laurent Gomila - SFML developer

Alidaco

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Sorry for the stupid question, but how do I store sockets?
« Reply #2 on: July 22, 2013, 02:13:38 pm »
So I should make a vector of smart pointers. Each smart pointer contains a normal pointer to a sf::TcpSocket which is initialized with a call to "new". Then I use the objects through the mart pointers and the sf::TcpSockets are deleted automatically when I delete a smart pointer off of the vector? I've never used smart pointers before so I just want to make sure that I'm understanding everything properly. Thanks a lot for your help!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sorry for the stupid question, but how do I store sockets?
« Reply #3 on: July 22, 2013, 02:16:11 pm »
If you use C++11, read some doc about std::shared_ptr and std::unique_ptr. If you don't, then there's no smart pointer class available to you, and you'll have to use a third-party library like boost.
Laurent Gomila - SFML developer

Alidaco

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Sorry for the stupid question, but how do I store sockets?
« Reply #4 on: July 22, 2013, 03:39:43 pm »
I think I'll just make a wrapper class for it that will handle the allocation and deletion with constructors and destructors. Thanks for your help! I just wanted to make sure that I wasn't missing the obvious.

 

anything