class variables: scoping

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neil Zanella

    class variables: scoping

    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
  • Leor Zolman

    #2
    Re: class variables: scoping

    On 3 Apr 2004 13:23:11 -0800, nzanella@cs.mun .ca (Neil Zanella) wrote:
    [color=blue]
    >Hello,
    >
    >Suppose I have some method:[/color]
    How about calling them "functions" now? You've been around for a while...
    ;-)
    [color=blue]
    >
    >Foo::foo() {
    > static int x;
    > int y;
    > /* ... */
    >}
    >
    >Here variables x is shared by all instances of the class,[/color]

    Not /that/ variable x, if you're referring to the one in Foo:foo(). That's
    a block-scope static, which means it can only be referred to within that
    function (and its value persists across multiple calls to it. By the way,
    it gets initialized to zero the first time the function is called, if you
    don't give it an initializer.)

    [color=blue]
    > but has
    >function scope. On the other hand, variable y, also has function
    >scope, but is reinitialized each time the method is called.[/color]

    Not by the code you've shown. Automatic locals without an initializer are
    left uninitialized.
    [color=blue]
    >
    >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.[/color]

    So far you've described a simple local automatic variable.
    [color=blue]
    >Essentially, I would like to have something like the equivalent of:
    >
    >class Foo {
    > /* ... */
    > private:
    > /* ... */
    > int z;
    > /* ... */
    >};[/color]

    Oh boy.
    [color=blue]
    >
    >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().[/color]

    The x you actually defined up there in foo() satisfies this (but not what
    you go on to specify below.)
    [color=blue]
    >
    >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]

    I /think/ I see what you want, but there's no primitive way to declare that
    in C++: You want an instance variable (one copy associated with each
    instance of an object) that is only in scope within a particular function,
    right? No can do. You'll just have to exercise some self-discipline by
    declaring a private data member and being careful to only access it from
    within the one function.
    [color=blue]
    >
    >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().[/color]

    Lots of things would be nice. This one isn't going to happen, though.[color=blue]
    >
    >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.[/color]

    To indicate to the compiler that it has to make room for it in each object
    instantiated. At least the way C++ is currently defined ;-)
    [color=blue]
    >It's like having to make
    >things global just because they are shared by instances.
    >Comments welcome.[/color]

    You got 'em.
    -leor
    [color=blue]
    >
    >Regards,
    >
    >Neil[/color]

    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

    Comment

    • Leor Zolman

      #3
      Re: class variables: scoping

      On 3 Apr 2004 13:23:11 -0800, nzanella@cs.mun .ca (Neil Zanella) wrote:
      [color=blue]
      >Hello,
      >
      >Suppose I have some method:[/color]
      How about calling them "functions" now? You've been around for a while...
      ;-)
      [color=blue]
      >
      >Foo::foo() {
      > static int x;
      > int y;
      > /* ... */
      >}
      >
      >Here variables x is shared by all instances of the class,[/color]

      Not /that/ variable x, if you're referring to the one in Foo:foo(). That's
      a block-scope static, which means it can only be referred to within that
      function (and its value persists across multiple calls to it. By the way,
      it gets initialized to zero the first time the function is called, if you
      don't give it an initializer.)

      [color=blue]
      > but has
      >function scope. On the other hand, variable y, also has function
      >scope, but is reinitialized each time the method is called.[/color]

      Not by the code you've shown. Automatic locals without an initializer are
      left uninitialized.
      [color=blue]
      >
      >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.[/color]

      So far you've described a simple local automatic variable.
      [color=blue]
      >Essentially, I would like to have something like the equivalent of:
      >
      >class Foo {
      > /* ... */
      > private:
      > /* ... */
      > int z;
      > /* ... */
      >};[/color]

      Oh boy.
      [color=blue]
      >
      >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().[/color]

      The x you actually defined up there in foo() satisfies this (but not what
      you go on to specify below.)
      [color=blue]
      >
      >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]

      I /think/ I see what you want, but there's no primitive way to declare that
      in C++: You want an instance variable (one copy associated with each
      instance of an object) that is only in scope within a particular function,
      right? No can do. You'll just have to exercise some self-discipline by
      declaring a private data member and being careful to only access it from
      within the one function.
      [color=blue]
      >
      >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().[/color]

      Lots of things would be nice. This one isn't going to happen, though.[color=blue]
      >
      >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.[/color]

      To indicate to the compiler that it has to make room for it in each object
      instantiated. At least the way C++ is currently defined ;-)
      [color=blue]
      >It's like having to make
      >things global just because they are shared by instances.
      >Comments welcome.[/color]

      You got 'em.
      -leor
      [color=blue]
      >
      >Regards,
      >
      >Neil[/color]

      --
      Leor Zolman --- BD Software --- www.bdsoft.com
      On-Site Training in C/C++, Java, Perl and Unix
      C++ users: Download BD Software's free STL Error Message Decryptor at:
      An STL Error Decryptor for C++ by Leor Zolman of BD Software - available to download here

      Comment

      • @(none)

        #4
        Re: class variables: scoping

        Neil Zanella wrote:
        [color=blue]
        > [...]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;
        > /* ... */
        > }[/color]

        If I understood correctly, you want to associate a variable (state) to a
        member function (behavior)... I would say you need a new object for that
        (and a class, of course). Your little object would have just one method,
        so I guess it's safe to call it a functor, maybe overwrite the "()"
        opertor to look more like a function...
        [color=blue]
        > 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.[/color]

        I'm not sure it is that common to justify inclusion in the language..
        and it's not that difficult to get the same effect using existing
        language constructs.

        regards,
        iuli

        Comment

        • @(none)

          #5
          Re: class variables: scoping

          Neil Zanella wrote:
          [color=blue]
          > [...]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;
          > /* ... */
          > }[/color]

          If I understood correctly, you want to associate a variable (state) to a
          member function (behavior)... I would say you need a new object for that
          (and a class, of course). Your little object would have just one method,
          so I guess it's safe to call it a functor, maybe overwrite the "()"
          opertor to look more like a function...
          [color=blue]
          > 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.[/color]

          I'm not sure it is that common to justify inclusion in the language..
          and it's not that difficult to get the same effect using existing
          language constructs.

          regards,
          iuli

          Comment

          • Siemel Naran

            #6
            Re: class variables: scoping

            "Neil Zanella" <nzanella@cs.mu n.ca> wrote in message
            [color=blue]
            > 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:[/color]

            Basically, a private data member accessible to one or more member functions.

            I tried the following once but it fails compile. But maybe we can tweak it
            a bit, and then it will compile? Maybe with the pointer to implementation
            concept it would work?

            class Foo;

            class Foo_f_Data
            {
            int x;
            friend void Foo::f(); // this line fails compile because class Foo not
            defined
            };

            class Foo : private Foo_f_Data
            {
            public:
            void f();
            void g();
            };

            void Foo::f() {
            x = 3; // ok
            }

            void Foo::g() {
            x = 5; // fails compile, c
            }

            Of course, comments are a nice idea too!


            Comment

            • Siemel Naran

              #7
              Re: class variables: scoping

              "Neil Zanella" <nzanella@cs.mu n.ca> wrote in message
              [color=blue]
              > 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:[/color]

              Basically, a private data member accessible to one or more member functions.

              I tried the following once but it fails compile. But maybe we can tweak it
              a bit, and then it will compile? Maybe with the pointer to implementation
              concept it would work?

              class Foo;

              class Foo_f_Data
              {
              int x;
              friend void Foo::f(); // this line fails compile because class Foo not
              defined
              };

              class Foo : private Foo_f_Data
              {
              public:
              void f();
              void g();
              };

              void Foo::f() {
              x = 3; // ok
              }

              void Foo::g() {
              x = 5; // fails compile, c
              }

              Of course, comments are a nice idea too!


              Comment

              • Alf P. Steinbach

                #8
                Re: class variables: scoping

                * nzanella@cs.mun .ca (Neil Zanella) schriebt:[color=blue]
                >
                > 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=blue]
                > 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().[/color]

                As I recall there is a proposal to add members with automatic back-
                pointers, like Java inner classes, but whether it will be adopted...

                --
                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

                • Alf P. Steinbach

                  #9
                  Re: class variables: scoping

                  * nzanella@cs.mun .ca (Neil Zanella) schriebt:[color=blue]
                  >
                  > 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=blue]
                  > 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().[/color]

                  As I recall there is a proposal to add members with automatic back-
                  pointers, like Java inner classes, but whether it will be adopted...

                  --
                  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

                  • Neil Zanella

                    #10
                    Re: class variables: scoping

                    Leor Zolman <leor@bdsoft.co m> wrote in message:
                    [color=blue][color=green]
                    > >Foo::foo() {
                    > > static int x;
                    > > int y;
                    > > /* ... */
                    > >}
                    > >
                    > >Here variable x is shared by all instances of the class,[/color]
                    >
                    > Not /that/ variable x, if you're referring to the one in Foo:foo(). That's
                    > a block-scope static, which means it can only be referred to within that
                    > function (and its value persists across multiple calls to it. By the way,
                    > it gets initialized to zero the first time the function is called, if you
                    > don't give it an initializer.)[/color]

                    Perhaps my explanation was somewhat inadequate. What you have stated in the
                    above paragraph is correct. Indeed, x can only be referred to from within
                    member function Foo::foo(). And indeed since it is static the compiler will
                    automatically arrange for it to be initialized to zero when no initializer
                    is present. What I meant to express by saying that x is shared by all
                    instances of Foo is exemplified in the following code:

                    #include <iostream>

                    class Foo {
                    public:
                    void foo() {
                    static int x;
                    std::cout << x++ << std::endl;
                    }
                    };

                    int main() {
                    Foo foo;
                    foo.foo();
                    foo.foo();
                    Foo bar;
                    bar.foo();
                    Foo foobar;
                    foobar.foo();
                    }

                    Output:

                    0
                    1
                    2
                    3

                    The goal is to achieve the output:

                    0
                    1
                    0
                    0

                    without changing the contents of main and without changing the contents
                    of the class definition (which seems impossible in C++, since it does not
                    support the feature I describe).
                    [color=blue][color=green]
                    > >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]
                    >
                    > I /think/ I see what you want, but there's no primitive way to declare that
                    > in C++: You want an instance variable (one copy associated with each
                    > instance of an object) that is only in scope within a particular function,[/color]

                    Correct. That is indeed the feature I was describing. Furthermore I was
                    poining out that C++ supports combinations:

                    (one copy per class, class scope) (static data members in class body)
                    (one copy per class, function scope) (static data members in function body)
                    (one copy per instance, class scope) (automatic data in class body)

                    but not the combination:

                    (one copy per instance, function scope)

                    I think this is poor design of the C++ language, because several functions
                    need have boolean variables called initialize which act as follows. Class
                    Foo has methods foo(), bar(), and foobar(). We don't want the constructor
                    to call these because they consume too much precious time, sometimes only
                    to initialize data members that are not needed later on. So we have the
                    functions foo(), bar(), and foobar() do the respective data member
                    initializations they need when they are called. Given that the
                    sets of data members they initialize are disjoint in the
                    given scenario, and that foo() may initialize more than
                    one data member, it makes sense to have a variable
                    which satisfies the missing C++ feature in order
                    to perform the initialization when foo() is called.
                    Alas, I need put such initializer variables inside
                    the function body.

                    I wonder if any languages have what I describe. Smalltalk?
                    [color=blue]
                    > right? No can do. You'll just have to exercise some self-discipline by
                    > declaring a private data member and being careful to only access it from
                    > within the one function.[/color]
                    [color=blue][color=green]
                    > >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.[/color]
                    >
                    > To indicate to the compiler that it has to make room for it in each object
                    > instantiated. At least the way C++ is currently defined ;-)[/color]

                    Hmmm... but the compiler went and fetched those static variables for
                    the BSS segment. I gues they belong to another data segment altogether
                    hence that is what makes it possible???

                    Regards,

                    Neil

                    Comment

                    • Neil Zanella

                      #11
                      Re: class variables: scoping

                      Leor Zolman <leor@bdsoft.co m> wrote in message:
                      [color=blue][color=green]
                      > >Foo::foo() {
                      > > static int x;
                      > > int y;
                      > > /* ... */
                      > >}
                      > >
                      > >Here variable x is shared by all instances of the class,[/color]
                      >
                      > Not /that/ variable x, if you're referring to the one in Foo:foo(). That's
                      > a block-scope static, which means it can only be referred to within that
                      > function (and its value persists across multiple calls to it. By the way,
                      > it gets initialized to zero the first time the function is called, if you
                      > don't give it an initializer.)[/color]

                      Perhaps my explanation was somewhat inadequate. What you have stated in the
                      above paragraph is correct. Indeed, x can only be referred to from within
                      member function Foo::foo(). And indeed since it is static the compiler will
                      automatically arrange for it to be initialized to zero when no initializer
                      is present. What I meant to express by saying that x is shared by all
                      instances of Foo is exemplified in the following code:

                      #include <iostream>

                      class Foo {
                      public:
                      void foo() {
                      static int x;
                      std::cout << x++ << std::endl;
                      }
                      };

                      int main() {
                      Foo foo;
                      foo.foo();
                      foo.foo();
                      Foo bar;
                      bar.foo();
                      Foo foobar;
                      foobar.foo();
                      }

                      Output:

                      0
                      1
                      2
                      3

                      The goal is to achieve the output:

                      0
                      1
                      0
                      0

                      without changing the contents of main and without changing the contents
                      of the class definition (which seems impossible in C++, since it does not
                      support the feature I describe).
                      [color=blue][color=green]
                      > >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]
                      >
                      > I /think/ I see what you want, but there's no primitive way to declare that
                      > in C++: You want an instance variable (one copy associated with each
                      > instance of an object) that is only in scope within a particular function,[/color]

                      Correct. That is indeed the feature I was describing. Furthermore I was
                      poining out that C++ supports combinations:

                      (one copy per class, class scope) (static data members in class body)
                      (one copy per class, function scope) (static data members in function body)
                      (one copy per instance, class scope) (automatic data in class body)

                      but not the combination:

                      (one copy per instance, function scope)

                      I think this is poor design of the C++ language, because several functions
                      need have boolean variables called initialize which act as follows. Class
                      Foo has methods foo(), bar(), and foobar(). We don't want the constructor
                      to call these because they consume too much precious time, sometimes only
                      to initialize data members that are not needed later on. So we have the
                      functions foo(), bar(), and foobar() do the respective data member
                      initializations they need when they are called. Given that the
                      sets of data members they initialize are disjoint in the
                      given scenario, and that foo() may initialize more than
                      one data member, it makes sense to have a variable
                      which satisfies the missing C++ feature in order
                      to perform the initialization when foo() is called.
                      Alas, I need put such initializer variables inside
                      the function body.

                      I wonder if any languages have what I describe. Smalltalk?
                      [color=blue]
                      > right? No can do. You'll just have to exercise some self-discipline by
                      > declaring a private data member and being careful to only access it from
                      > within the one function.[/color]
                      [color=blue][color=green]
                      > >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.[/color]
                      >
                      > To indicate to the compiler that it has to make room for it in each object
                      > instantiated. At least the way C++ is currently defined ;-)[/color]

                      Hmmm... but the compiler went and fetched those static variables for
                      the BSS segment. I gues they belong to another data segment altogether
                      hence that is what makes it possible???

                      Regards,

                      Neil

                      Comment

                      • Gary Labowitz

                        #12
                        Re: class variables: scoping

                        "Neil Zanella" <nzanella@cs.mu n.ca> wrote in message
                        news:b68d2f19.0 404041224.5c8fe 744@posting.goo gle.com...[color=blue]
                        > I think this is poor design of the C++ language, because several[/color]
                        functions[color=blue]
                        > need have boolean variables called initialize which act as follows.[/color]
                        Class[color=blue]
                        > Foo has methods foo(), bar(), and foobar(). We don't want the[/color]
                        constructor[color=blue]
                        > to call these because they consume too much precious time, sometimes[/color]
                        only[color=blue]
                        > to initialize data members that are not needed later on. So we have[/color]
                        the[color=blue]
                        > functions foo(), bar(), and foobar() do the respective data member
                        > initializations they need when they are called. Given that the
                        > sets of data members they initialize are disjoint in the
                        > given scenario, and that foo() may initialize more than
                        > one data member, it makes sense to have a variable
                        > which satisfies the missing C++ feature in order
                        > to perform the initialization when foo() is called.
                        > Alas, I need put such initializer variables inside
                        > the function body.[/color]

                        I'm afraid I don't get it. If all three functions need to know if any
                        of the other's have already run, wouldn't a common variable to all
                        functions serve the purpose; i.e. a member in the class? If each of
                        the functions needed to know just which of the functions has already
                        run, then three statics named fooran, barran, and foobarran would
                        serve.
                        Is this right?
                        --
                        Gary


                        Comment

                        • Gary Labowitz

                          #13
                          Re: class variables: scoping

                          "Neil Zanella" <nzanella@cs.mu n.ca> wrote in message
                          news:b68d2f19.0 404041224.5c8fe 744@posting.goo gle.com...[color=blue]
                          > I think this is poor design of the C++ language, because several[/color]
                          functions[color=blue]
                          > need have boolean variables called initialize which act as follows.[/color]
                          Class[color=blue]
                          > Foo has methods foo(), bar(), and foobar(). We don't want the[/color]
                          constructor[color=blue]
                          > to call these because they consume too much precious time, sometimes[/color]
                          only[color=blue]
                          > to initialize data members that are not needed later on. So we have[/color]
                          the[color=blue]
                          > functions foo(), bar(), and foobar() do the respective data member
                          > initializations they need when they are called. Given that the
                          > sets of data members they initialize are disjoint in the
                          > given scenario, and that foo() may initialize more than
                          > one data member, it makes sense to have a variable
                          > which satisfies the missing C++ feature in order
                          > to perform the initialization when foo() is called.
                          > Alas, I need put such initializer variables inside
                          > the function body.[/color]

                          I'm afraid I don't get it. If all three functions need to know if any
                          of the other's have already run, wouldn't a common variable to all
                          functions serve the purpose; i.e. a member in the class? If each of
                          the functions needed to know just which of the functions has already
                          run, then three statics named fooran, barran, and foobarran would
                          serve.
                          Is this right?
                          --
                          Gary


                          Comment

                          • Neil Zanella

                            #14
                            Re: class variables: scoping

                            alfps@start.no (Alf P. Steinbach) wrote in message news:<406fde7b. 193238015@news. individual.net> ...[color=blue]
                            > * nzanella@cs.mun .ca (Neil Zanella) schriebt:[color=green]
                            > >
                            > > 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..

                            I am not sure how your example would help in the situation I describe.
                            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.

                            #include <iostream>
                            #include <cstdlib>

                            class Foo {
                            public:
                            Foo() { init(); }
                            Foo(int y): y(y) { init(); }
                            void init() {
                            /* defer initialization until needed */
                            runFoo1 = runFoo2 = runFoo3 = runFoo4 = true;
                            }
                            int foo1() { if (runFoo1) x1 = compute(0, 0); return x1; }
                            int foo2() { if (runFoo2) x2 = compute(0, 1); return x2; }
                            int foo3() { if (runFoo3) x3 = compute(1, 0); return x3; }
                            int foo4() { if (runFoo4) x4 = compute(1, 1); return x4; }
                            private:
                            int compute(int a, int b) {
                            /* suppose this takes a long time to run */
                            return rand() + 2 * a + b; /* for simplicity */
                            }
                            int x1, x2, x3, x4;
                            int y;
                            private:
                            bool runFoo1, runFoo2, runFoo3, runFoo4;
                            };

                            int main() {

                            /* run program without waiting for unnecessary computations */
                            /* available which would unnecessarily initialize class data */

                            Foo foo1;
                            std::cout << foo1.foo1() << std::endl;

                            /* foo1.foo2(), foo1.foo3(), and foo1.foo4() never called */
                            /* hence OO program can run as fast as a procedural program */

                            Foo foo2;
                            std::cout << foo2.foo2() << std::endl;
                            std::cout << foo2.foo3() << std::endl;
                            std::cout << foo2.foo4() << std::endl;

                            /* foo1.foo1() never called */
                            /* hence OO program can run as fast as a procedural program */

                            }

                            Comment

                            • Neil Zanella

                              #15
                              Re: class variables: scoping

                              alfps@start.no (Alf P. Steinbach) wrote in message news:<406fde7b. 193238015@news. individual.net> ...[color=blue]
                              > * nzanella@cs.mun .ca (Neil Zanella) schriebt:[color=green]
                              > >
                              > > 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..

                              I am not sure how your example would help in the situation I describe.
                              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.

                              #include <iostream>
                              #include <cstdlib>

                              class Foo {
                              public:
                              Foo() { init(); }
                              Foo(int y): y(y) { init(); }
                              void init() {
                              /* defer initialization until needed */
                              runFoo1 = runFoo2 = runFoo3 = runFoo4 = true;
                              }
                              int foo1() { if (runFoo1) x1 = compute(0, 0); return x1; }
                              int foo2() { if (runFoo2) x2 = compute(0, 1); return x2; }
                              int foo3() { if (runFoo3) x3 = compute(1, 0); return x3; }
                              int foo4() { if (runFoo4) x4 = compute(1, 1); return x4; }
                              private:
                              int compute(int a, int b) {
                              /* suppose this takes a long time to run */
                              return rand() + 2 * a + b; /* for simplicity */
                              }
                              int x1, x2, x3, x4;
                              int y;
                              private:
                              bool runFoo1, runFoo2, runFoo3, runFoo4;
                              };

                              int main() {

                              /* run program without waiting for unnecessary computations */
                              /* available which would unnecessarily initialize class data */

                              Foo foo1;
                              std::cout << foo1.foo1() << std::endl;

                              /* foo1.foo2(), foo1.foo3(), and foo1.foo4() never called */
                              /* hence OO program can run as fast as a procedural program */

                              Foo foo2;
                              std::cout << foo2.foo2() << std::endl;
                              std::cout << foo2.foo3() << std::endl;
                              std::cout << foo2.foo4() << std::endl;

                              /* foo1.foo1() never called */
                              /* hence OO program can run as fast as a procedural program */

                              }

                              Comment

                              Working...