Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > static class local variable in memeber function

Reply
Thread Tools

static class local variable in memeber function

 
 
john
Guest
Posts: n/a
 
      08-12-2005
By doing:

void MyClass::MyFunction()
{
static int myvar;
....
}

We can defined a function local variable 'myvar' that keeps its value
from call to call (the point is that the variable can not be easily
accessed from outside the function, *that* is what we want to achieve)

But this variable is global to *all* instance of the MyClass class. Is
there an artifact that allow a similar definition but for a variable
that is instance-based.

?
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      08-12-2005
john wrote:

> By doing:
>
> void MyClass::MyFunction()
> {
> static int myvar;
> ...
> }
>
> We can defined a function local variable 'myvar' that keeps its value
> from call to call (the point is that the variable can not be easily
> accessed from outside the function, *that* is what we want to achieve)
>
> But this variable is global to *all* instance of the MyClass class. Is
> there an artifact that allow a similar definition but for a variable
> that is instance-based.
>
> ?


Make it a member variable.

 
Reply With Quote
 
 
 
 
msalters
Guest
Posts: n/a
 
      08-12-2005

john schreef:

> By doing:
>
> void MyClass::MyFunction()
> {
> static int myvar;
> ...
> }
>
> We can defined a function local variable 'myvar' that keeps its value
> from call to call (the point is that the variable can not be easily
> accessed from outside the function, *that* is what we want to achieve)
>
> But this variable is global to *all* instance of the MyClass class. Is
> there an artifact that allow a similar definition but for a variable
> that is instance-based.


std::map<MyClass*, int> myvars;
myvars[this] = ...

HTH,
Michiel Salters

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-12-2005
* john:
> By doing:
>
> void MyClass::MyFunction()
> {
> static int myvar;
> ...
> }
>
> We can defined a function local variable 'myvar' that keeps its value
> from call to call (the point is that the variable can not be easily
> accessed from outside the function, *that* is what we want to achieve)
>
> But this variable is global to *all* instance of the MyClass class. Is
> there an artifact that allow a similar definition but for a variable
> that is instance-based.
>
> ?


Not really.

But consider that if 'myvar' is to be restricted to a _single_ function,
then it is a local variable.

And if it is to be restricted to a _set_ of member functions, then the
functionality it partakes in is also restricted to that set, so the set
forms its own class. I.e. you can make that set of member functions, plus
variable, a separate class that 'MyClass' inherits from. Or that 'MyClass'
contains an instance variable of.

--
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?
 
Reply With Quote
 
john
Guest
Posts: n/a
 
      08-12-2005
Rolf Magnus a écrit :
> john wrote:
>
>
>>By doing:
>>
>>void MyClass::MyFunction()
>>{
>>static int myvar;
>>...
>>}
>>
>>We can defined a function local variable 'myvar' that keeps its value
>>from call to call (the point is that the variable can not be easily
>>accessed from outside the function, *that* is what we want to achieve)
>>
>>But this variable is global to *all* instance of the MyClass class. Is
>>there an artifact that allow a similar definition but for a variable
>>that is instance-based.
>>
>>?

>
>
> Make it a member variable.
>

Well and it can be accedeed from outside the function. you did not
understand the point.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-12-2005
john wrote:
> Rolf Magnus a écrit :
>
>> john wrote:
>>
>>
>>> By doing:
>>>
>>> void MyClass::MyFunction()
>>> {
>>> static int myvar;
>>> ...
>>> }
>>>
>>> We can defined a function local variable 'myvar' that keeps its value
>>> from call to call (the point is that the variable can not be easily
>>> accessed from outside the function, *that* is what we want to achieve)
>>>
>>> But this variable is global to *all* instance of the MyClass class. Is
>>> there an artifact that allow a similar definition but for a variable
>>> that is instance-based.
>>>
>>> ?

>>
>>
>>
>> Make it a member variable.
>>

> Well and it can be accedeed from outside the function.


So what? Make it private, so only members of this class can use it.

> you did not
> understand the point.


What problem are you trying to solve and why would a private non-static
member variable not work?

V
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Calling memeber function pointer witha constant object. puzzlecracker C++ 3 03-11-2009 08:10 PM
Access of a local class in a template function to a static variable ymost@hotmail.com C++ 2 02-04-2009 09:46 AM
Connection String of Default Memeber Store Ryan ASP .Net 0 02-13-2007 11:47 PM
object memeber initialization josh C++ 5 02-13-2007 02:41 AM
a static local variable in a static method is thread local storage? Patrick Hoffmann C++ 3 08-08-2003 02:37 PM



Advertisments
 



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 47 48 49 50 51 52 53 54 55 56 57