linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Shriramana Sharma <samjnaa@gmail.com>
To: Linux C Programming List <linux-c-programming@vger.kernel.org>
Subject: need non-classwide sticky variable for a member function of a class
Date: Fri, 22 Jun 2007 13:28:55 +0530	[thread overview]
Message-ID: <467B813F.4060408@gmail.com> (raw)

[-- 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" ;
}

             reply	other threads:[~2007-06-22  7:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-06-22  7:58 Shriramana Sharma [this message]
2007-06-23 15:59 ` need non-classwide sticky variable for a member function of a class Glynn Clements
2007-06-23 18:33 ` Benoît Rouits

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=467B813F.4060408@gmail.com \
    --to=samjnaa@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).