N
Neil Zanella
Hello,
Suppose I have some method:
Foo::foo() {
static int x;
int y;
/* ... */
}
Here variables x is shared by all instances of the class, but has
function scope. On the other hand, variable y, also has function
scope, but is reinitialized each time the method is called.
Now, I would like to have a variable which has function scope,
that is, is not visible by other class functions. However, every
time I instantiate an instance of Foo a new such variable is created.
Essentially, I would like to have something like the equivalent of:
class Foo {
/* ... */
private:
/* ... */
int z;
/* ... */
};
However, I do not want to have all methods inside Foo see z. I only
want method foo to see it, just like I did for x which can only be
seen by foo().
The value of z should survive different calls from within the same
instance, but should be initialized for each new instance, like above.
But it should have method scope, not class scope.
It would be nice if C++ were extended to include this nice idea.
It could for example allow something like:
Foo::foo() {
static int x;
int y;
semistatic z;
/* ... */
}
Where semistatic means: shared by all invocations within the same
instance, and only visible within foo().
I think this feature is missing from C++ and is quite useful. Because
if some class variable is used only in one method, then why should I
have to place it in the class definition. It's like having to make
things global just because they are shared by instances.
Comments welcome.
Regards,
Neil
Suppose I have some method:
Foo::foo() {
static int x;
int y;
/* ... */
}
Here variables x is shared by all instances of the class, but has
function scope. On the other hand, variable y, also has function
scope, but is reinitialized each time the method is called.
Now, I would like to have a variable which has function scope,
that is, is not visible by other class functions. However, every
time I instantiate an instance of Foo a new such variable is created.
Essentially, I would like to have something like the equivalent of:
class Foo {
/* ... */
private:
/* ... */
int z;
/* ... */
};
However, I do not want to have all methods inside Foo see z. I only
want method foo to see it, just like I did for x which can only be
seen by foo().
The value of z should survive different calls from within the same
instance, but should be initialized for each new instance, like above.
But it should have method scope, not class scope.
It would be nice if C++ were extended to include this nice idea.
It could for example allow something like:
Foo::foo() {
static int x;
int y;
semistatic z;
/* ... */
}
Where semistatic means: shared by all invocations within the same
instance, and only visible within foo().
I think this feature is missing from C++ and is quite useful. Because
if some class variable is used only in one method, then why should I
have to place it in the class definition. It's like having to make
things global just because they are shared by instances.
Comments welcome.
Regards,
Neil