Hey there, I have a few questions about the #define's being used in this code, I never use them in my own code, and I'm not exactly sure how the following ones work, so I'd like to see what I can do differently. I read that std::function might be an alternative to some #defines, and I know that it's bad practice to use them in C++ which is why I want to get rid of them.
Here are some examples:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
#define A( s ) #s
#define OPTION(type, var, val) Var<type> var = {A(var), val}
template <typename T = bool>
class Var {
public:
std::string name;
std::shared_ptr<T> value;
int32_t size;
Var(std::string name, T v) : name(name) {
value = std::make_shared<T>(v);
size = sizeof(T);
}
operator T() { return *value; }
operator T*() { return &*value; }
operator T() const { return *value; }
};
|
So this basically has two defines at the top, and the way that the defines are being used is like:
OPTION(bool, isMale, true)
I tried tracing this, and what it seems to be the equivalent to is:
|
Var<bool> isMale = {#isMale, true};
|
which makes no sense to me. The main part that is confusing me is the:
I don't really know why that is used, and for all i know, it just replaces s with #s. Does this pound symbol have some special meaning inside of the define? What could I change this to, in order to get the same result?
Here's my next example:
1 2 3 4 5
|
#define NETVAR(type, name, table, netvar) \
type& name##() const { \
static int _##name = NetvarSys::Get().GetOffset(table, netvar); \
return *(type*)((uintptr_t)this + _##name); \
}
|
and this is used to make functions inside of a class.
If you were to say something like this:
|
NETVAR(int32_t, test, "table", "variable")
|
would it be the same as the following?
1 2 3 4
|
int32_t test() const{
static int a = NetvarSys::Get().GetOffset("table", "variable");
return *(int32_t*)((uintptr_t)this + a);
}
|
Edit: also removing the backslashes at the end of each line in the second example breaks it. No clue what those do either.
Thanks for any help, i'd really like to get rid of all these #defines and use something else in their place, but the first step is really just understanding why they are being used like this.