* need non-classwide sticky variable for a member function of a class
@ 2007-06-22 7:58 Shriramana Sharma
2007-06-23 15:59 ` Glynn Clements
2007-06-23 18:33 ` Benoît Rouits
0 siblings, 2 replies; 3+ messages in thread
From: Shriramana Sharma @ 2007-06-22 7:58 UTC (permalink / raw)
To: Linux C Programming List
[-- Attachment #1: Type: text/plain, Size: 1969 bytes --]
Hello.
Definition of the problem :
* I have a class representing a time object with a property
startingPoint accessed by the member function startingPoint().
* I do not want to calculate the value of the property for each class
instance at the time of creation of the instance, since that calculation
is computationally intensive.
* Only if someone asks for the value I want to calculate it.
* Once calculated, I want to store it to be returned directly from
storage if it is asked for again.
* Different instances of the class of course have different
startingPoint values. So it will require that there is a separate store
variable for the startingPoint property of each instance.
I first thought I could use a static variable inside the function
startingPoint() which would act as the store. But the compiler outputs
only one block of machine code for the function, which means there is
actually only one static store created. This means that there are no
separate stores for each class instance.
Of course the workaround is obvious -- declare a private variable
directly as a member of the class instead of a static variable in the
member function.
What I really want to ask is, aren't the two meanings of the keyword
static mixed up here?
When a class has a static member, it means that that member belongs to
the whole class. Different instances of a class can use this to pass
information amongst themselves, for example.
But here there is a variable which is a static "member" of a non-static
member function of a class, and it is also acting as a classwide object.
To my mind, there are three meanings to the keyword static:
1. local visibility (vars and funcs)
2. classwide commonness (vars and funcs)
3. sticky nature (vars only) to hold values between function calls
Here the second and third meanings are mixed up -- that makes the static
keyword quite ambiguous, don't you think?
Your comments invited.
Shriramana Sharma.
[-- Attachment #2: class-function-static-var.cpp --]
[-- Type: text/x-c++src, Size: 1042 bytes --]
# include <iostream>
using namespace std ;
class Number
{
public :
Number ( int i ) : i_ ( i ) {}
int value ( void ) { return i_ ; }
int previous ( void ) ;
private :
int i_ ;
} ;
int Number :: previous ( void )
{
static int previousValue ;
static bool calculated = false ;
if ( not calculated )
{
cout << "Not calculated.\nCalculating...\n" ;
previousValue = i_ - 1 ;
calculated = true ;
cout << "Returning calculated value.\n" ;
return previousValue ;
}
// else
cout << "Already calculated.\nNot calculating again.\n" ;
cout << "Returning previously calculated value.\n" ;
return previousValue ;
}
int main ( void )
{
Number a ( 100 ) ;
cout << "a :\n" ;
cout << a . value () ;
cout << "\na.previous :\n" ;
cout << a . previous () ;
cout << "\nAgain a.previous :\n" ;
cout << a . previous () ;
Number b ( 200 ) ;
cout << "\nb :\n" ;
cout << b . value () ;
cout << "\nb.previous :\n" ;
cout << b . previous () ;
cout << "\nAgain b.previous :\n" ;
cout << b . previous () ;
cout << "\n" ;
}
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: need non-classwide sticky variable for a member function of a class 2007-06-22 7:58 need non-classwide sticky variable for a member function of a class Shriramana Sharma @ 2007-06-23 15:59 ` Glynn Clements 2007-06-23 18:33 ` Benoît Rouits 1 sibling, 0 replies; 3+ messages in thread From: Glynn Clements @ 2007-06-23 15:59 UTC (permalink / raw) To: Shriramana Sharma; +Cc: Linux C Programming List Shriramana Sharma wrote: > * Different instances of the class of course have different > startingPoint values. So it will require that there is a separate store > variable for the startingPoint property of each instance. > > I first thought I could use a static variable inside the function > startingPoint() which would act as the store. But the compiler outputs > only one block of machine code for the function, which means there is > actually only one static store created. This means that there are no > separate stores for each class instance. > > Of course the workaround is obvious -- declare a private variable > directly as a member of the class instead of a static variable in the > member function. > > What I really want to ask is, aren't the two meanings of the keyword > static mixed up here? No. All of the other different meanings are mixed up, but both of these are (historically) correct usage. > When a class has a static member, it means that that member belongs to > the whole class. Different instances of a class can use this to pass > information amongst themselves, for example. > > But here there is a variable which is a static "member" No it isn't. It's a static local variable, not a member. > of a non-static member function of a class, and it is also acting as > a classwide object. For member functions, "static" means that the function doesn't require an instance. This is a "mixed up" use of the term. In the historical sense of the term, all functions are static, in that they reside at a fixed address. > To my mind, there are three meanings to the keyword static: > > 1. local visibility (vars and funcs) This is compatible with C's misuse of "static" to mean "not exported". > 2. classwide commonness (vars and funcs) Static member variables are static variables in the traditional sense of the term. They're only member variables insofar as the name is local to the class (i.e. has to be prefixed with "classname::" if referenced from outside a member function). Static methods (member functions) only differ from non-static methods insofar as they don't require an instance. > 3. sticky nature (vars only) to hold values between function calls This is the traditional use of "static", i.e. a single occurrence residing at a static address, as opposed to an automatic variable which resides within the function's stack frame, and has a separate instance for each invocation (in the case of recursion or multi-threading), or a dynamic variable which is allocated on the heap (by malloc(), "new", etc). > Here the second and third meanings are mixed up -- that makes the static > keyword quite ambiguous, don't you think? All three are mixed up. #3 (and #2 when referring to variables) is the historical usage, but re-using the "static" keyword for other purposes is a C tradition which has been expanded upon in C++. There are three completely separate issues here: storage (where the variable is stored), scope (what a specific name refers to in a specific context) and linkage (what a specific name refers to from the linker's perspectvie). Historically, the term "static" referred to storage. C functions, global variables, "static" top-level variables and "static" local variables, and C++ methods and "static" member variables are all static in terms of storage. OTOH, C local variables are automatic by default; you can explicitly declare them as "auto" but, as that's the default, no-one ever does (I don't think I've ever seen the "auto" keyword used in real code). Finally, C++ member variables are essentially structure fields. Whether their storage is static, automatic or dynamic depends upon whether the instance is static, automatic or dynamic. -- Glynn Clements <glynn@gclements.plus.com> ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: need non-classwide sticky variable for a member function of a class 2007-06-22 7:58 need non-classwide sticky variable for a member function of a class Shriramana Sharma 2007-06-23 15:59 ` Glynn Clements @ 2007-06-23 18:33 ` Benoît Rouits 1 sibling, 0 replies; 3+ messages in thread From: Benoît Rouits @ 2007-06-23 18:33 UTC (permalink / raw) To: Shriramana Sharma; +Cc: Linux C Programming List Le vendredi 22 juin 2007 à 13:28 +0530, Shriramana Sharma a écrit : > To my mind, there are three meanings to the keyword static: > > 1. local visibility (vars and funcs) > 2. classwide commonness (vars and funcs) > 3. sticky nature (vars only) to hold values between function calls > > Here the second and third meanings are mixed up -- that makes the > static keyword quite ambiguous, don't you think? I agree, "static" has two meanings among one for a local variable. > > Your comments invited. > > Shriramana Sharma. - To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2007-06-23 18:33 UTC | newest] Thread overview: 3+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2007-06-22 7:58 need non-classwide sticky variable for a member function of a class Shriramana Sharma 2007-06-23 15:59 ` Glynn Clements 2007-06-23 18:33 ` Benoît Rouits
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).