creating static array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rahul Joshi

    creating static array

    Hi,

    I want to create a static array whose size is also a const
    member of the class. Something like:

    // A.h
    class A {
    private:
    static int size = 0;
    static int array[size];
    };

    Then I need to define the static array in A.cpp like:

    int A::array[A::size];

    However, the compiler complains that A::size is a private
    and cannot be accessed. So how can I define such a static
    array without making the size of the array public ?

    Thanks,
    Rahul

  • Andrey Tarasevich

    #2
    Re: creating static array

    Rahul Joshi wrote:[color=blue]
    > ...
    > I want to create a static array whose size is also a const
    > member of the class. Something like:
    >
    > // A.h
    > class A {
    > private:
    > static int size = 0;
    > static int array[size];
    > };
    >
    > Then I need to define the static array in A.cpp like:
    >
    > int A::array[A::size];
    >
    > However, the compiler complains that A::size is a private
    > and cannot be accessed. So how can I define such a static
    > array without making the size of the array public ?
    > ...[/color]

    Aside from missing 'const' specifier and array size being zero, I don't
    see any problems with this code. (Please, always post _real_ code, i.e.
    the code you are actually trying to compile.)

    The error message is most likely caused by a bug in your compiler.
    According to 11/5 access control is not supposed to restrict access to
    'A::size' in the above member definition.

    --
    Best regards,
    Andrey Tarasevich
    Brainbench C and C++ Programming MVP

    Comment

    • xiaoqing Visson

      #3
      Re: creating static array

      i think you just make a mistake when you declare
      static int array[size] ;
      because std c++ doesn't support "dynamic" declare , you know size is not a
      const number, so this will generate a mistake. and i compile it in dev-c++
      4.9.8.0 with winme,
      it just appear "4 D:\source code\stl_c++\st atictest.h
      size of member `array' is not constant"
      so if you want to achive this ,i think the best way is to use stl: vector.


      "Rahul Joshi" <rujoshi@studen ts.uiuc.edu>
      ??????:Pine.GSO .4.31.030717191 8510.7422-100000@ux7.cso. uiuc.edu...[color=blue]
      > Hi,
      >
      > I want to create a static array whose size is also a const
      > member of the class. Something like:
      >
      > // A.h
      > class A {
      > private:
      > static int size = 0;
      > static int array[size];
      > };
      >[/color]
      [color=blue]
      > Then I need to define the static array in A.cpp like:
      >
      > int A::array[A::size];
      >
      > However, the compiler complains that A::size is a private
      > and cannot be accessed. So how can I define such a static
      > array without making the size of the array public ?
      >
      > Thanks,
      > Rahul
      >[/color]




      Comment

      • Rahul Joshi

        #4
        Re: creating static array



        On Thu, 17 Jul 2003, Andrey Tarasevich wrote:
        [color=blue]
        > Rahul Joshi wrote:[color=green]
        > > ...
        > > I want to create a static array whose size is also a const
        > > member of the class. Something like:
        > >
        > > // A.h
        > > class A {
        > > private:
        > > static int size = 0;
        > > static int array[size];
        > > };
        > >
        > > Then I need to define the static array in A.cpp like:
        > >
        > > int A::array[A::size];
        > >
        > > However, the compiler complains that A::size is a private
        > > and cannot be accessed. So how can I define such a static
        > > array without making the size of the array public ?
        > > ...[/color]
        >
        > Aside from missing 'const' specifier and array size being zero, I don't
        > see any problems with this code. (Please, always post _real_ code, i.e.
        > the code you are actually trying to compile.)
        >
        > The error message is most likely caused by a bug in your compiler.
        > According to 11/5 access control is not supposed to restrict access to
        > 'A::size' in the above member definition.
        >
        > --
        > Best regards,
        > Andrey Tarasevich
        > Brainbench C and C++ Programming MVP
        >
        >[/color]

        Sorry. That was some code I made up. I was trying to define a static array
        of pointers-to-member functions of the class, and the size of this array
        is a static const int. Apparently, my syntax was wrong. As suggested in
        C++ FAQ, I used a typedef for the pointer-to-member type, and it compiled
        correctly. Thanks.

        Rahul

        Comment

        Working...