static class local variable in memeber function

J

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.

?
 
R

Rolf Magnus

john said:
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.
 
M

msalters

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
 
A

Alf P. Steinbach

* 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.
 
V

Victor Bazarov

john said:
Rolf Magnus a écrit :

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top