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

Author Topic: sf::NonCopyable  (Read 2296 times)

0 Members and 1 Guest are viewing this topic.

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
sf::NonCopyable
« on: November 02, 2015, 03:41:10 am »
void acceptClient(sf::TcpListener& server)
{

}
int main()
{
    sf::TcpListener server;
   
    sf::Thread thread(&acceptClient,server); //'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is private
}

is the only way to pass classes that inheirts sf::NonCopyable class is to pass it by pointer?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
sf::NonCopyable
« Reply #1 on: November 02, 2015, 07:57:22 am »
No, you can pass a reference or a smart pointer as well.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

lefreut

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: sf::NonCopyable
« Reply #2 on: November 02, 2015, 04:28:37 pm »
In this case, you need to pass std::ref(server) to sf::Thread to explicitly tell to the compiler that the argument is a reference. Otherwise, the compiler tries to perform a copy.

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: sf::NonCopyable
« Reply #3 on: November 03, 2015, 09:04:29 am »
struct A
{
    sf::TcpListener server;
    sf::TcpSocket socket;
};

A object = { std::ref(listener), std::ref(socket) };

error:
use of deleted function 'sf::TcpListener::TcpListener(const sf::TcpListener&)

how do i do it correctly?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::NonCopyable
« Reply #4 on: November 03, 2015, 10:09:33 am »
If you have access to std::ref(), it's very likely that you can use std::thread as well. You should prefer standard threads where possible.

You don't need to pass reference wrappers to your object, but to the thread.
std::thread(&function, std::ref(arg));

And please have a look at a C++ reference before asking questions about how to use the standard library.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything