I cant figure out what solution is better as I don't clearly understand the difference. I want to have a file that includes some variables that can be freely used throughout the program. My first attempt was the following:
Header file (Hosts.h)
#pragma once
#include <string>
namespace Some::Namespace {
using std::string;
class Hosts {
public:
static const unsigned int SOME_DEVICE_PORT;
static const string SOME_DEVICE_IP;
};
}
Implementation file (Hosts.cpp)
#include <Network/Hosts.h>
namespace Some::Namespace {
const unsigned int Hosts::SOME_DEVICE_PORT = 10100;
const string Hosts::SOME_DEVICE_IP = "192.168.1.1";
}
Then I became aware of the inline keyword which would make the implementation file redundant and you will be able to define the variables within the header file alone - just like this:
Header file with inline (Hosts.h)
#pragma once
#include <string>
namespace Some::Namespace {
using std::string;
class Hosts {
public:
static const unsigned int SOME_DEVICE_PORT = 10100;
inline static const string SOME_DEVICE_IP = "192.168.1.1";
};
}
Now my question is: How does these implementations differs in the matter of number of variable instances? Preferable I would like there to only be one instance of every variable. But will either of these solutions do this and are there other pitfalls regarding optimisation?
Thanks