0

I'm reading the source code of godot, and I can't understand how NetSocket class creates its instance.

In net_socket.cpp, create() is defined, but I can't see how it works.

#include "net_socket.h"

NetSocket *(*NetSocket::_create)() = NULL;

NetSocket *NetSocket::create() {

    if (_create)
        return _create();

    ERR_PRINT("Unable to create network socket, platform not supported");
    return NULL;
}

Especially, what is _create? And what does NetSocket *(*NetSocket::_create)() = NULL; do actually?

42milez
  • 23
  • 2
  • 1
    Related: https://stackoverflow.com/questions/35654908/explanation-of-function-pointers - not exactly a duplicate, but it should get you started :) – Caramiriel Jan 19 '19 at 13:13
  • Please show `_create()` - without it the question is not answerable. – Jesper Juhl Jan 19 '19 at 13:17
  • _@42milez_ Check where in the source code the `NetSocket::_create` function pointer is set to a specific function. It sounds like there are several platform specific implementations for that function (most probably in classes derived from `NetSocket`). – πάντα ῥεῖ Jan 19 '19 at 13:38

1 Answers1

2

You're looking at the wrong file. That one just delegates to whatever platform-specific implementation has been linked in/instantiated, using a function pointer called _create.

It is set in, for example, the POSIX impl.

Simply search the codebase for instances of _create and you'll see how it works.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055