E
Eric
I have a static class member variable as follows:
struct A
{
static void Set (int i) { v = i; }
static int& Get () { return v; }
static int v;
};
int A::v; // define A::v in the cpp file
A::v will have external linkage and there will only be one instance of this
variable in the executable:
However, let's say I don't want to define the variable in the cpp file (eg.
it's a template class and I don't want users to have to define the
variable).
Is there anything wrong with defining it in a static member function which
returns a reference to the variable? Will there also always only be one copy
of the local static variable? Any other unforseen problems? Thanks in
advance for any comments.
struct A
{
static void Set (int i) { v() = i; }
static int& Get () { return v(); }
static int& v()
{
static v;
return v;
}
};
struct A
{
static void Set (int i) { v = i; }
static int& Get () { return v; }
static int v;
};
int A::v; // define A::v in the cpp file
A::v will have external linkage and there will only be one instance of this
variable in the executable:
However, let's say I don't want to define the variable in the cpp file (eg.
it's a template class and I don't want users to have to define the
variable).
Is there anything wrong with defining it in a static member function which
returns a reference to the variable? Will there also always only be one copy
of the local static variable? Any other unforseen problems? Thanks in
advance for any comments.
struct A
{
static void Set (int i) { v() = i; }
static int& Get () { return v(); }
static int& v()
{
static v;
return v;
}
};