473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

About static member variable

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!

Sep 29 '07 #1
13 2261
ya*********@gmail.com wrote:
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?
Simple. Don't use any static variables.

If you can asnwer the following questions, you can answer your own:

"I like walking outside when it's raining, and stay dry, but I
don't want to wear any rain gear or carry an umbrella. How to
accomplish my goal?"

"I want to live a long life and be healthy, but I don't want to
give up smoking or alcohol or drugs. How can I do that?"

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 29 '07 #2
Thanks for all your funny answers! :D

It sounds that I have asked for way to an unreachable thing. But I
have found such requirements in real life indeed, while I also have
some "wacky" habit (such as writing class implement in header file
only).

There is a example in Microsoft's ATL. I found class CWindow has a
static member rcDefault. Microsoft use its own keyword
"_declspec(selectany)" to slove this problem. I am curious if there is
a common solution via C++ language itself.

Sep 29 '07 #3
On Sep 29, 7:10 pm, yanlinli...@gmail.com wrote:
I'd like to write all code of a class in only header file.
Why? The header file is visible by all users; the more you put
in it, the more coupling you get, and the harder it is to
maintain the program.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 1 '07 #4
James Kanze <james.ka...@gmail.comwrote:
On Sep 29, 7:10 pm, yanlinli...@gmail.com wrote:
I'd like to write all code of a class in only header file.

Why? The header file is visible by all users; the more you put
in it, the more coupling you get, and the harder it is to
maintain the program.
I don't think that putting all code in header file would increase the
coupling. Since the implementation code is private in class, users can
ignore them usually.

The reason I do this is from the occasion that I publish my code to
others in Windows platform. There are many compiling types for library
(such as multi-threading, multi-threading DLL), and every type can
build as both Debug and Release (as well as ANSI or Unicode, etc.). As
a result, I have to give so many different .lib files. If I publish
my .h and .cpp files, troubles also can be met. Target projects always
need to be changed, to add my .cpp file as one compiling unit. So, I
publish only header files finally, they can only use #include to use
my code now.

Oct 1 '07 #5
On Oct 1, 3:06 pm, yanlinli...@gmail.com wrote:
James Kanze <james.ka...@gmail.comwrote:
On Sep 29, 7:10 pm, yanlinli...@gmail.com wrote:
I'd like to write all code of a class in only header file.
Why? The header file is visible by all users; the more you put
in it, the more coupling you get, and the harder it is to
maintain the program.
I don't think that putting all code in header file would increase the
coupling. Since the implementation code is private in class, users can
ignore them usually.
Never the less, the granularity of the build system is usually
the file; modify the header, and you trigger the recompilation
of the client code; modify a separate source, and you don't. Or
wourse, if for some reason you don't recompile part of the code,
you get inconsistancies.
The reason I do this is from the occasion that I publish my code to
others in Windows platform. There are many compiling types for library
(such as multi-threading, multi-threading DLL), and every type can
build as both Debug and Release (as well as ANSI or Unicode, etc.). As
a result, I have to give so many different .lib files. If I publish
my .h and .cpp files, troubles also can be met. Target projects always
need to be changed, to add my .cpp file as one compiling unit. So, I
publish only header files finally, they can only use #include to use
my code now.
If you want to offer all of the options, you almost have to
provide source, and let the user build the library. Most
libraries I know don't, however. They provide a single .so or
..a, depending on whether they are designed to be linked
dynamically or statically, and the necessary headers.
Typically, you won't have debugging information in the library,
but this is not a problem, because you won't have to debug it.
(What you deliver to the customer is always a "release" build.
By definition.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 2 '07 #6
Alf P. Steinbach wrote:
Constants are not problematic, just use 'const':

const int x = 42;
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?

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).
Oct 2 '07 #7
Juha Nieminen wrote:
Alf P. Steinbach wrote:
>Constants are not problematic, just use 'const':

const int x = 42;

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?

AFAIK not having an actual instance of such a variable can be
problematic if someone takes a pointer to it.
I belive you mean "obtains its address". The actual wording I think
I've seen is "uses it in the context that treats it as an l-value",
which could mean taking its address, initialising a reference with
it, etc. (although I am not sure what's in 'etc')
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).
I believe you're bringing up the ODR, which should hold for the program
to be a standard C++ program.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 2 '07 #8
On 2007-10-02 07:10:13 -1000, Juha Nieminen <no****@thanks.invalidsaid:
Alf P. Steinbach wrote:
>Constants are not problematic, just use 'const':

const int x = 42;

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).
The language definition uses the term "static members" to refer to,
well, static members. They are static and they are members. C++ does
not have the formal notion of "class variable", and using that term
leads to confusion.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 2 '07 #9
Pete Becker wrote:
The language definition uses the term "static members" to refer to,
well, static members. They are static and they are members. C++ does not
have the formal notion of "class variable", and using that term leads to
confusion.
Well, I believe that the distinction between "member functions" and
"class functions" is quite common. Why shouldn't the same distinction be
made with variables?
Oct 2 '07 #10
Juha Nieminen wrote:
Pete Becker wrote:
>The language definition uses the term "static members" to refer to,
well, static members. They are static and they are members. C++ does
not have the formal notion of "class variable", and using that term
leads to confusion.

Well, I believe that the distinction between "member functions" and
"class functions" is quite common. Why shouldn't the same distinction
be made with variables?
I've not seen "class functions" up until your post. What does it mean?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 2 '07 #11
On 2007-10-02 09:11:38 -1000, Juha Nieminen <no****@thanks.invalidsaid:
Pete Becker wrote:
>The language definition uses the term "static members" to refer to,
well, static members. They are static and they are members. C++ does not
have the formal notion of "class variable", and using that term leads to
confusion.

Well, I believe that the distinction between "member functions" and
"class functions" is quite common. Why shouldn't the same distinction be
made with variables?
It's not about making the distinction, but about what you call it. The
language definition uses the term "static member" to refer to static
members, both functions (as in "static member function") and data (as
in "static member data"). Don't invent new terms.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 3 '07 #12
On Sep 29, 7:10 pm, yanlinli...@gmail.com wrote:
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 <thissolution
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

Oct 3 '07 #13
On Oct 2, 7:10 pm, Juha Nieminen <nos...@thanks.invalidwrote:
Alf P. Steinbach wrote:
Constants are not problematic, just use 'const':
const int x = 42;
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 3 '07 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: IHateSuperman | last post by:
public class StaticField2{ public static void main(String args){ private int x, y; // <<== error 1 for ( y = 0 ; y < 100 ; y++){ x = StaticMethod(); System.out.println(" x = "+x); } } public...
29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
16
by: Eric | last post by:
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
4
by: Serge | last post by:
Hi, I have no problem creating a static member variable with integers, etc but when I try the same with a vector then I always get linker errors that the static member variable is unknown...
7
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this...
10
by: Rene | last post by:
I jus realized that I can change the values of "static variables" and "instance variable" through the standard constructor This means that something like this will compile: public class...
2
by: newjazzharmony | last post by:
All, I'd appreciate it if someone could answer these questions for me. This scenario applies to an ASP dot net application that is deployed in a web farm. 1) Does the application live in a...
5
by: John Goche | last post by:
Hello, I would like to know whethere there is a difference between a const variable and a static const variable inside a class. After all, if a variable is const in a class, the compiler can...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.