About static member variable

W

werasm

I'd like to write all code of a class in only header file. But when
there is any static member varia ble, I have to define it in cpp file,
not the header file. Otherwise, the static member variable may be
duplicated because two or more cpp files may include the header.
How can I solve this without the cpp file, which used to define the
static member variable?
Thanks!

I agree with most of what everyone has said, therefore <this> solution
to your problem is not worth much I deem, but it may be considered:

template <class T, class U>
struct HasStaticVar
{
static U get;
};

template <class T,class U>
U HasStaticVar<T,U>::get = U();

struct X : HasStaticVar<X,int>
{
};

int main()
{
X::get = 20;
return 0;
}

Of course, you could make the static member protected
so as to only allow change via derived. If the member
is something other than builtin, you would have to use
interesting schemes to initialize it to something other
than its default. etc. Nevertheless, X has its static
variable.

Kind regards,

Werner
 
J

James Kanze

He was talking about *member* variable (well, a static "member"
variable would technically not be a member variable but a class
variable, but I assume that with "member variable" he meant "class
variable", ie. a static variable inside the scope of a class).
You can't write that for member variables (well, at least not until
the next C++ standard). You can, however, write this:
static const int x = 42;
I'm wondering though: What does the standard say about such static
const variables? Do they need an actual instance or not?

According to the current standard, you need an actual instance
if the variable is ever used. According to the latest draft,
you need an actual instance if the variable is ever used in a
context where it is not immediately subject to an lvalue to
rvalue conversion.

In practice, if you want to be sure that your code will work,
you have to provide an instance.
AFAIK not having an actual instance of such a variable can be
problematic if someone takes a pointer to it. A pointer requires that
the variable has an actual physical instance. Moreover, IIRC all
pointers which reference the same variable should be identical (ie. it's
not ok for the compiler to simply instantiate the variable in the
current compilation unit if the code creates a pointer to the variable,
because all such pointers in different compilation units must point to
the same memory location, if I'm not completely mistaken).

That's exact. The compiler cannot generate more than one
instance of the variable, and still be conform. For all the
compilers I know, if the compiler needs an instance, you must
provide a definition somewhere.
 
K

Kai-Uwe Bux

James said:
I should have been clearer. I was talking about compile time
coupling. Quite frankly, it didn't even occur to me that human
beings would read a header file (other than to modify it).

I totally misinterpreted your statement. (In hindsight, I should have been
able to do a better job. After all, you had gone on record already with the
observation that client code programmers should have no reason to peek into
headers.) I'm sorry for the noise.


Best

Kai-Uwe Bux
 
Joined
Oct 4, 2007
Messages
4
Reaction score
0
Why don't you try dedicating one CPP file for the purpose?
Add a CPP file by the name of your choice. Define it there.
Some times we have to live with evil situations. Live with lesser of evils!!!!!!!!
Compiler will warn you in case you define it some where else.
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top