Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - About static member variable

 
Thread Tools Search this Thread
Old 09-29-2007, 06:10 PM   #1
Default 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!



yanlinlin82@gmail.com
  Reply With Quote
Old 09-29-2007, 06:16 PM   #2
Victor Bazarov
 
Posts: n/a
Default Re: About static member variable
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




Victor Bazarov
  Reply With Quote
Old 09-29-2007, 06:22 PM   #3
Alf P. Steinbach
 
Posts: n/a
Default Re: 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?


The best way is to avoid static variables, except constants: using them
is an all too common beginner's mistake, much like using goto.

Constants are not problematic, just use 'const':

const int x = 42;

Here you don't need to be concerned about duplication because a
namespace scope constant has internal linkage by default.

That said, technical solutions to non-const static variable in headers
include using local statics (wrapping in functions) and templatizing.

But best, just say no: you can be nearly 100% sure that it's a mistake.


Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Alf P. Steinbach
  Reply With Quote
Old 09-29-2007, 06:24 PM   #4
Alf P. Steinbach
 
Posts: n/a
Default Re: About static member variable
* Victor Bazarov:
>
> "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?"


It seems that yogurt is key.

At least in Kazakhstan and places like that.

Cheers, & hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Alf P. Steinbach
  Reply With Quote
Old 09-29-2007, 08:55 PM   #5
yanlinlin82@gmail.com
 
Posts: n/a
Default Re: About static member variable
Thanks for all your funny answers!

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.



yanlinlin82@gmail.com
  Reply With Quote
Old 10-01-2007, 09:10 AM   #6
James Kanze
 
Posts: n/a
Default Re: About static member variable
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:
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



James Kanze
  Reply With Quote
Old 10-01-2007, 09:33 AM   #7
Kai-Uwe Bux
 
Posts: n/a
Default Re: About static member variable
James Kanze wrote:

> 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.


There may be thousands of good reasons to separate header files from
implementation files, but that one, I do not consider convincing.

A header is meant for the compiler only. Users refer to the documentation.
They have no business looking at the header. There is no coupling coming
from this (unless users prefer to rely on undocumented features, which is
entirely their problem).

Likewise: you don't get coupling from the standard headers should they be
readable on a particular implementation. Why would you from another header?


Best

Kai-Uwe Bux


Kai-Uwe Bux
  Reply With Quote
Old 10-01-2007, 10:29 AM   #8
Ian Collins
 
Posts: n/a
Default Re: About static member variable
Kai-Uwe Bux wrote:
> James Kanze wrote:
>
>> 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.

>
> There may be thousands of good reasons to separate header files from
> implementation files, but that one, I do not consider convincing.
>

This time, I agree with James.

> A header is meant for the compiler only. Users refer to the documentation.
> They have no business looking at the header. There is no coupling coming
> from this (unless users prefer to rely on undocumented features, which is
> entirely their problem).
>

Maybe, but headers have a strange way of finding themselves included in
compilation units, the "lower level" the header, the more places both
directly and indirectly it gets included. If there is code in the
header and the project is under development, that code is likely to get
changed, which results in all those compilation units being recompiled.

> Likewise: you don't get coupling from the standard headers should they be
> readable on a particular implementation. Why would you from another header?
>

You don't change your standard headers.

--
Ian Collins.


Ian Collins
  Reply With Quote
Old 10-01-2007, 10:46 AM   #9
Kai-Uwe Bux
 
Posts: n/a
Default Re: About static member variable
Ian Collins wrote:

> Kai-Uwe Bux wrote:
>> James Kanze wrote:
>>
>>> 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.

>>
>> There may be thousands of good reasons to separate header files from
>> implementation files, but that one, I do not consider convincing.
>>

> This time, I agree with James.


With the recommendation, or with the reasoning? I did not dispute the
former.

>> A header is meant for the compiler only. Users refer to the
>> documentation. They have no business looking at the header. There is no
>> coupling coming from this (unless users prefer to rely on undocumented
>> features, which is entirely their problem).
>>

> Maybe, but headers have a strange way of finding themselves included in
> compilation units, the "lower level" the header, the more places both
> directly and indirectly it gets included. If there is code in the
> header and the project is under development, that code is likely to get
> changed, which results in all those compilation units being recompiled.


That is a one of the thousand other reasons I acknowledged. But maybe, I
just misunderstood what James had in mind when he was saying "visible by
all users". I took that to refer to human users not files that include the
header.

>> Likewise: you don't get coupling from the standard headers should they be
>> readable on a particular implementation. Why would you from another
>> header?
>>

> You don't change your standard headers.


True. But they are visible by all users

I thought, it was clear from my topic sentence that I have no problem with
the recommendation per se to put the implementation into a different file.
I just question the particular rationale.


Best

Kai-Uwe Bux


Kai-Uwe Bux
  Reply With Quote
Old 10-01-2007, 02:06 PM   #10
yanlinlin82@gmail.com
 
Posts: n/a
Default Re: About static member variable
James Kanze <james.ka...@gmail.com> wrote:
> 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.



yanlinlin82@gmail.com
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cisco 2620 static natting help biomed32uk Hardware 1 10-22-2009 03:25 PM
please help: simple java coding error 'cannot be referenced from a static context' clm90 General Help Related Topics 0 10-17-2009 06:49 AM
Passing value with out using variable in query string in PHP! Ali_ggl General Help Related Topics 0 11-29-2008 12:22 PM
Shag Floor danger? Dave Hardenbrook A+ Certification 3 02-28-2008 05:13 AM
Variable Scope in asp.Net jansi_rk Software 1 09-18-2006 06:05 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46