SFML community forums

Help => Network => Topic started by: Alidaco on July 22, 2013, 12:55:07 pm

Title: [SOLVED] Sorry for the stupid question, but how do I store sockets?
Post by: Alidaco on July 22, 2013, 12:55:07 pm
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.
Title: Re: Sorry for the stupid question, but how do I store sockets?
Post by: Laurent 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)
Title: Re: Sorry for the stupid question, but how do I store sockets?
Post by: Alidaco 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!
Title: Re: Sorry for the stupid question, but how do I store sockets?
Post by: Laurent 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.
Title: Re: Sorry for the stupid question, but how do I store sockets?
Post by: Alidaco 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.