[immodule-qt] keyword static of C++ (Re: Question about svn trunk)

YamaKen yamaken at bp.iij4u.or.jp
Tue Aug 10 20:00:12 EEST 2004


Hi all, this is my last TODO item currently doable. Please reply
to any discussions to activate me :)

At Tue, 13 Jul 2004 02:46:53 +0900,
yamaken at bp.iij4u.or.jp wrote:
> 
> At Mon, 12 Jul 2004 23:19:18 +0900,
> kaminmat at cc.rim.or.jp wrote:
> > 
> > On Monday 12 July 2004 06:41, YamaKen wrote:
> > > Probably you have forgotten the
> > > code. QETWidget::translateKeyEventInternal() uses a static
> > > variable instead of instance variable to store the state as
> > > following, so lastWinId is required to prevent interwidget key
> > > state transition.
> > 
> > I understood that the static variable in a method was separately 
> > treated for each instance. lastWinId will be required if this understanding 
> > is wrong. Please commit the code.
> 
> At least, gcc 3.3.3 showed that my understanding is correct. See
> attached code and following result.
> 
> $ gcc --version
> gcc (GCC) 3.3.3 [FreeBSD] 20031106
> $ ./a.out 
> 0xbfbfe7cf: initial
> 0xbfbfe7ce: initial
> 0xbfbfe7cf: a
> 0xbfbfe7ce: a
> 
> But I had recently seen some C++ codes based on your
> understanding. Is it a new C++ feature? Or, is some famous
> (wrong) explanation about it existing?

Daisuke and Cougar (and possibly Kazuki) are understanding
static variable within a method as per-instance storage. But I
can't imagine that such feature is a correct C++ specification
because it breaks linker semantics or costs unnatural constructs
through vptr.

I've investigated the C++ specification to verify this
issue. But I've not found any description about such feature.

First I've updated my C++ specification doc from 'ISO/IEC 14882
First edition 1998-09-01' to 'ISO/IEC 14882 Second edition
2003-10-15' by purchasing from ANSI eStandards Store.

http://webstore.ansi.org/ansidocstore/product.asp?sku=INCITS%2FISO%2FIEC+14882%2D2003

And I had read about the static. But I had found only
traditional definitions about it. No other definitions around
'declared static' or 'keyword static' had been found by fulltext
search.

----------------------------------------------------------------
7 Declarations
7.1.1 Storage class specifiers
4 The static specifier can be applied only to names of objects
  and functions and to anonymous unions (9.5). There can be no
  static function declarations within a block, nor any static
  function parameters. A static specifier used in the
  declaration of an object declares the object to have static
  storage duration (3.7.1). A static specifier can be used in
  declarations of class members; 9.4 describes its effect. For
  the linkage of a name declared with a static specifier, see
  3.5.

3.7 Storage duration
3.7.1 Static storage duration
1 All objects which neither have dynamic storage duration nor
  are local have static storage duration. The storage for these
  objects shall last for the duration of the program (3.6.2,
  3.6.3).

2 If an object of static storage duration has initialization or
  a destructor with side effects, it shall not be eliminated
  even if it appears to be unused, except that a class object or
  its copy may be eliminated as specified in 12.8.

3 The keyword static can be used to declare a local variable
  with static storage duration. [Note: 6.7 describes the
  initialization of local static variables; 3.6.3 describes the
  destruction of local static variables. ]

4 The keyword static applied to a class data member in a class
  definition gives the data member static storage duration.
----------------------------------------------------------------


In addition, I had googled about this issue and found a wiki
page.

Cpp Static Riddle
http://c2.com/cgi/wiki?CppStaticRiddle

----------------------------------------------------------------
I've got a question on this topic. Is a static variable defined
in a class method, similar to a member variable that has scope
only for that method? In other words is a new static variable
created for each instance of the class?

No, see above for the meaning of static method variable and
static instance variable.
----------------------------------------------------------------


These two descriptions in this mail and the behavior of gcc
3.3.3 written in my previous mail are indicating that my
understanding is right.

I want to rewrite some codes in our repository involving static
variables if nobody shows me another information.

Anyone?

-------------------------------
YamaKen  yamaken at bp.iij4u.or.jp



More information about the immodule-qt mailing list