Skip to content

Prefer to use static constexpr or static const instead of #define #3

@ASmoliak

Description

@ASmoliak

image

In server.hpp, there is a definition of MAX_SESSIONS
It is in global scope, any source or header file that includes this header will now have the definition of MAX_SESSIONS in their namespace, yet they will have nothing to do with it because it's an implementation detail of the server class

You should ditch the #define, and instead just declare it as a private member of the server class:
static contsexpr size_t kMaxSessions = 10
or
static const size_t kMaxSessions = 10

The advantage of this method:

  1. Can view the variable in a debugger
  2. Encapsulation: users outside of the class can no longer see this

Disadvantage is that some functions in server.cpp will not be able to access it because they're not all members of the server class, in this case you will have to add them as static functions

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions