Topic: Static variables in member functions
Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Thu, 24 Nov 1994 03:29:23 GMT Raw View
In article <3assbv$lsb@ztivax.zfe.siemens.de> michi@km21.zfe.siemens.de (Michael Klug) writes:
>|> There is only one member function f.
>
>Right and wrong.
>
>Right, if the _implementation_ is considered: the code of f is parameterized
>with object member addresses used in f's body (since each instance of a class
>gets its own set of data member).
>
>Wrong, if language semantics is considered. instance_a.f and instance_b.f are
>not identical, since they operate on different data members. A global `a'
>referred to in instance_a.f is not identical to the `a' referred to in
>instance_b.f. Of course, they have the identical semantics, but they _are_ not
>identical.
Its the same global a. But an 'x' in the object is
a different x for each function , yes.
So you are correct that the _closure_ of f with an object
is distinct for each object -- and lament the fact you cannot
have variables which accept this kind of function (or pointer
thereto).
Semantically, the language is confused. The working
paper claims a member function's type is the same as
an ordinary function (supporting what you say) and then
makes the "address" thereof a completely different kind of
entity (pointer to member) which shows the language is confused.
Given this -- dont be so sure you are right, at least
from the "official" point of view :-)
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd,
81A Glebe Point Rd, GLEBE Mem: SA IT/9/22,SC22/WG21
NSW 2037, AUSTRALIA Phone: 61-2-566-2189
Author: stal@km21.zfe.siemens.de (Michael Stal)
Date: 18 Nov 1994 14:18:25 GMT Raw View
Since we recently had a discussion regarding static variables in member
functions, I have a question.
Consider the following trivial code:
class X {
public:
void f(void);
}
void X::f(void)
{
static int i = 42;
cout << i << " \n";
i += 42;
}
int main(void)
{
X x1, x2;
}
This small example will result in an output like "42 84".
One part of C++-programmers told me, that they would expect the
output "42 42" instead, cos' they thought every instance of X gets
its own instance of the member function variable i. The ANSI Draft
states that there is only one instance of i for all class instances.
Author: etxbrfa@kk417 (ETX-T-BEA Fahller Bjorn)
Date: 18 Nov 1994 15:45:41 GMT Raw View
Michael Stal (stal@km21.zfe.siemens.de) wrote:
[code example with static member variable]
: One part of C++-programmers told me, that they would expect the
: output "42 42" instead, cos' they thought every instance of X gets
: its own instance of the member function variable i. The ANSI Draft
: states that there is only one instance of i for all class instances.
: From an implementors point of view both interpretations would be
: easy to implement. So, what was the motivation behind this definition
: of "static"???
If every instance of the class got its own instance of the static member
variable, I don't see what functionality the keyword 'static' would
have for static member variables. It'd just be like any other member
variable.
_
/Bjorn.
--
H:c/o Eklund W:Ericsson Telecom etxbrfa@kk.ericsson.se
Kvistbrogatan 15 kk/etx/t/bea <Team-OS/2>
S-124 67 Bandhagen/SWEDEN S-126 25 Stockholm/Sweden Software designers work
Phone: +46 8 647 4374 Phone: +46 8 719 9620 with vapour-ware.
Author: michi@km21.zfe.siemens.de (Michael Klug)
Date: 21 Nov 1994 17:47:13 GMT Raw View
In article <3aii76$mu6@erinews.ericsson.se>, etxbrfa@kk417 (ETX-T-BEA Fahller Bjorn) writes:
|> Michael Stal (stal@km21.zfe.siemens.de) wrote:
|>
|> [code example with static member variable]
|>
|> : One part of C++-programmers told me, that they would expect the
|> : output "42 42" instead, cos' they thought every instance of X gets
|> : its own instance of the member function variable i. The ANSI Draft
|> : states that there is only one instance of i for all class instances.
|>
|> : From an implementors point of view both interpretations would be
|> : easy to implement. So, what was the motivation behind this definition
|> : of "static"???
|>
|> If every instance of the class got its own instance of the static member
|> variable, I don't see what functionality the keyword 'static' would
|> have for static member variables. It'd just be like any other member
|> variable.
Just imagine a method which has to/wants to count the number of invocations.
If the local static variables were not identical, it could be done with
such a variable. With the existing semantics in C++, the variable will count
the method calls for _all_ existing instances of the class. This is (another)
stupid feature of C++. Just tell me one good reason ...
--MiKl
Author: maxtal@physics.su.OZ.AU (John Max Skaller)
Date: Mon, 21 Nov 1994 21:17:57 GMT Raw View
In article <3aid3h$l0i@ztivax.zfe.siemens.de> stal@km21.zfe.siemens.de (Michael Stal) writes:
>Since we recently had a discussion regarding static variables in member
>functions, I have a question.
>Consider the following trivial code:
>
>class X {
>public:
> void f(void);
>}
>
>void X::f(void)
>{
> static int i = 42;
> cout << i << " \n";
> i += 42;
>}
>
>int main(void)
>{
> X x1, x2;
>}
>
>This small example will result in an output like "42 84".
>
>One part of C++-programmers told me, that they would expect the
>output "42 42" instead, cos' they thought every instance of X gets
>its own instance of the member function variable i. The ANSI Draft
ANSI/ISO draft please. And you're not even American!
>states that there is only one instance of i for all class instances.
>
>From an implementors point of view both interpretations would be
>easy to implement. So, what was the motivation behind this definition
>of "static"???
There is only one member function f.
Variance between objects would imply "i" was a non-static
member of X -- I have no idea how that would be implemented.
Note that only one instance of "i" per program is permitted,
whether or not "f" is inline. "f" has external linkage, all member
functions of non-local classes do (since San Diego). This is not
quite so easy to implement.
--
JOHN (MAX) SKALLER, INTERNET:maxtal@suphys.physics.su.oz.au
Maxtal Pty Ltd,
81A Glebe Point Rd, GLEBE Mem: SA IT/9/22,SC22/WG21
NSW 2037, AUSTRALIA Phone: 61-2-566-2189
Author: tob@world.std.com (Tom O Breton)
Date: Mon, 21 Nov 1994 22:07:24 GMT Raw View
michi@km21.zfe.siemens.de (Michael Klug) writes:
> Just imagine a method which has to/wants to count the number of invocations.
> If the local static variables were not identical, it could be done with
> such a variable. With the existing semantics in C++, the variable will count
> the method calls for _all_ existing instances of the class. This is (another)
> stupid feature of C++. Just tell me one good reason ...
I'm not sure exactly what you want, but every variation I can think of
can be done within the language without much trouble. Count the number
of times it's created through one specific invocation? Keep a static
count there, pass it. Count the number of invocations total, not
counting duplicates? That's the toughest case (and I really don't see
the point), but you can pass __FILE__,__LINE__ and keep a list to reject
duplicates. Count total number of instances? Trivial.
C++ has some misfeatures, as you say, but IMO this is not one.
Tom
--
tob@world.std.com
TomBreton@delphi.com: Author of The Burning Tower
Author: michi@km21.zfe.siemens.de (Michael Klug)
Date: 22 Nov 1994 13:40:15 GMT Raw View
In article <CzMz5y.EyG@ucc.su.OZ.AU>, maxtal@physics.su.OZ.AU (John Max Skaller) writes:
|> In article <3aid3h$l0i@ztivax.zfe.siemens.de> stal@km21.zfe.siemens.de (Michael Stal) writes:
|> >Since we recently had a discussion regarding static variables in member
|> >functions, I have a question.
|> >Consider the following trivial code:
|> >
|> >class X {
|> >public:
|> > void f(void);
|> >}
|> >
|> >void X::f(void)
|> >{
|> > static int i = 42;
|> > cout << i << " \n";
|> > i += 42;
|> >}
|> >
|> >int main(void)
|> >{
|> > X x1, x2;
|> >}
|> >
|> >This small example will result in an output like "42 84".
|> >
|> >One part of C++-programmers told me, that they would expect the
|> >output "42 42" instead, cos' they thought every instance of X gets
|> >its own instance of the member function variable i. The ANSI Draft
|>
|> ANSI/ISO draft please. And you're not even American!
|>
|> >states that there is only one instance of i for all class instances.
|> >
|> >From an implementors point of view both interpretations would be
|> >easy to implement. So, what was the motivation behind this definition
|> >of "static"???
|>
|> There is only one member function f.
Right and wrong.
Right, if the _implementation_ is considered: the code of f is parameterized
with object member addresses used in f's body (since each instance of a class
gets its own set of data member).
Wrong, if language semantics is considered. instance_a.f and instance_b.f are
not identical, since they operate on different data members. A global `a'
referred to in instance_a.f is not identical to the `a' referred to in
instance_b.f. Of course, they have the identical semantics, but they _are_ not
identical.
MiKl
--
________________________________________________________________________________
SIEMENS AG Michael Klug
Corporate Research & Development Tel: +49 89 636-43414
ZFE T SE 3 Fax: +49 89 636-40757
81730 Munich, Germany Email: Michael.Klug@zfe.siemens.de
________________________________________________________________________________
Author: barmar@nic.near.net (Barry Margolin)
Date: 22 Nov 1994 17:05:23 -0500 Raw View
In article <3aqmf1$2mr@ztivax.zfe.siemens.de> michi@km21.zfe.siemens.de (Michael Klug) writes:
>Just imagine a method which has to/wants to count the number of invocations.
>If the local static variables were not identical, it could be done with
>such a variable. With the existing semantics in C++, the variable will count
>the method calls for _all_ existing instances of the class. This is (another)
>stupid feature of C++. Just tell me one good reason ...
If you want per-instance variables, that's what non-static member variables
are for. If static variables in member functions were per-instance, the
obvious implementation would be to store them in the instance. However,
the language is currently designed so that the size of an instance can be
determined simply from the class declaration.
It could be done by requiring all instances to have a pointer to a
per-instance statics table, or a variable-length tail to hold per-instance
statics. The former would require instance creation and destruction to
allocate/free a dynamic table, and the latter would require instance
creation to be sized at runtime. This would be overhead that all classes
would incur, even if they don't have any statics in member functions; this
is contrary to the C++ philosophy.
--
Barry Margolin
BBN Internet Services Corp.
barmar@near.net