Re: class variables: scoping
* nzanella@cs.mun .ca (Neil Zanella) schriebt:[color=blue]
> alfps@start.no (Alf P. Steinbach) wrote in message news:<406fde7b. 193238015@news. individual.net> ...[color=green]
> > * nzanella@cs.mun .ca (Neil Zanella) schriebt:[color=darkred]
> > >
> > > 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.[/color]
> >
> >
> > class Foo;
> >
> > class FooFunctor
> > {
> > private:
> > Foo& myFoo;
> > int myZ;
> > public:
> > FooFunctor( Foo& aFoo ): myFoo( aFoo ), myZ( 0 ) {}
> > int operator()();
> > };
> >
> > class Foo
> > {
> > private:
> > int x;
> > FooFunctor foo;
> > public:
> > Foo(): x(0), foo(*this) {}
> >
> > void bar()
> > {
> > x = foo();
> > }
> > };
> >
> > int FooFunctor::ope rator()(){ ... }[/color]
>
> Thank you for your reply..[/color]
You're welcome..
[color=blue]
> I am not sure how your example would help in the situation I describe.[/color]
Try it.
[color=blue]
> Consider the following code example where having the runFooi variables
> being local to the current function would be of practical use. These
> would then also have to be initialized within that function with
> a scheme similar to that of the initialization of static variables:
> on first call for an instance the variable is constructed,
> subsequent calls within same object ignore the
> construction and use the old value
> for current object.[/color]
That's the same problem again, and it's no more difficult with four
variables than with one.
In addition you have added an irrelevant aspect, namly logical constness.
Look up the keyword 'mutable' for that.
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
* nzanella@cs.mun .ca (Neil Zanella) schriebt:[color=blue]
> alfps@start.no (Alf P. Steinbach) wrote in message news:<406fde7b. 193238015@news. individual.net> ...[color=green]
> > * nzanella@cs.mun .ca (Neil Zanella) schriebt:[color=darkred]
> > >
> > > 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.[/color]
> >
> >
> > class Foo;
> >
> > class FooFunctor
> > {
> > private:
> > Foo& myFoo;
> > int myZ;
> > public:
> > FooFunctor( Foo& aFoo ): myFoo( aFoo ), myZ( 0 ) {}
> > int operator()();
> > };
> >
> > class Foo
> > {
> > private:
> > int x;
> > FooFunctor foo;
> > public:
> > Foo(): x(0), foo(*this) {}
> >
> > void bar()
> > {
> > x = foo();
> > }
> > };
> >
> > int FooFunctor::ope rator()(){ ... }[/color]
>
> Thank you for your reply..[/color]
You're welcome..
[color=blue]
> I am not sure how your example would help in the situation I describe.[/color]
Try it.
[color=blue]
> Consider the following code example where having the runFooi variables
> being local to the current function would be of practical use. These
> would then also have to be initialized within that function with
> a scheme similar to that of the initialization of static variables:
> on first call for an instance the variable is constructed,
> subsequent calls within same object ignore the
> construction and use the old value
> for current object.[/color]
That's the same problem again, and it's no more difficult with four
variables than with one.
In addition you have added an irrelevant aspect, namly logical constness.
Look up the keyword 'mutable' for that.
--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Comment