Example: server constructor
In this first example, we are going to leverage what we’ve learned so far to create flexible constructors for data types. We will also see how we can create constructors with default values of our choosing.
In our setup, a Server
struct is a simple struct that has a set number of maximum connections, a transport type, and a name. We won’t be building an actual web server, but rather, we are demonstrating the concepts with only a small amount of overhead. What we want to do in this example is to focus on the core ideas, which you can then apply anywhere you see fit. Our server only has three configurable parameters, but you can imagine that this benefit is more pronounced when there are more parameters to configure.
As always, we are going to start by defining the custom types of our application. To keep it lightweight, I’m defining two of them – TransportType
, which is an int
type to be used as an enumeration, and a type...