const array declaration/init for a class

Discussion in 'C++' started by Victor Hannak, Sep 20, 2003.

  1. I have a class that needs to reference a const array in several of its
    methods. Where should I put the declaration/initialization of this array so
    that it is only created once when the class is instantiated, but is visible
    to all of the methods of only this class ?

    const unsigned short ConstantVectorWidth = 10;
    const float ConstantVector[ConstantVectorWidth] =
    {5,10,15,20,25,30,35,40,45,50};

    Does this syntax look correct?
    Victor Hannak, Sep 20, 2003
    #1
    1. Advertising

  2. Victor Hannak wrote:

    > I have a class that needs to reference a const array in several of its
    > methods. Where should I put the declaration/initialization of this array so
    > that it is only created once when the class is instantiated, but is visible
    > to all of the methods of only this class ?
    >
    > const unsigned short ConstantVectorWidth = 10;
    > const float ConstantVector[ConstantVectorWidth] =
    > {5,10,15,20,25,30,35,40,45,50};
    >
    > Does this syntax look correct?


    No.

    *** in the header file:

    class SomeClass
    {
    static const short ConstantVectorWidth = 10; /* won't work on some
    compilers */
    static const float ConstantVector[];
    }

    *** in the implementation file:

    // if initialization inside the class doesn't work, do it here:
    // const short SomeClass::ConstantVectorWidth = 10;
    const float SomeClass::ConstantVector[SomeClass::ConstantVectorWidth] =
    { ... };

    I *think* that's right, anyway...

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Sep 20, 2003
    #2
    1. Advertising

  3. Victor Hannak

    WW Guest

    Kevin Goodsell wrote:
    > *** in the header file:
    >
    > class SomeClass
    > {
    > static const short ConstantVectorWidth = 10; /* won't work on some
    > compilers */
    > static const float ConstantVector[];
    > }
    >
    > *** in the implementation file:
    >
    > // if initialization inside the class doesn't work, do it here:
    > // const short SomeClass::ConstantVectorWidth = 10;
    > const float SomeClass::ConstantVector[SomeClass::ConstantVectorWidth]
    > = { ... };
    >
    > I *think* that's right, anyway...


    You can even completely hide it inside an unnamed namespace of the
    implementation file - of course as long as you do not need to refer to them
    from inline functions in the header.

    --
    WW aka Attila
    WW, Sep 20, 2003
    #3
  4. I'm still having problems with this...

    "Kevin Goodsell" <> wrote in message
    news:bCRab.10711$...
    > *** in the header file:
    >
    > class SomeClass
    > {
    > static const short ConstantVectorWidth = 10; /* won't work on some
    > compilers */
    > static const float ConstantVector[];
    > }
    >


    Do these statements need to be in the public portion of the class
    declaration? There is no public: or private: tags above, does that imply
    public?

    > *** in the implementation file:
    >
    > // if initialization inside the class doesn't work, do it here:
    > // const short SomeClass::ConstantVectorWidth = 10;
    > const float SomeClass::ConstantVector[SomeClass::ConstantVectorWidth] =
    > { ... };
    >


    This causes the error "Identifier 'ConstantArray' cannot have a type
    qualifier". If I take out the type qualifier, i.e.

    const float ConstantVector[SomeClass::ConstantVectorWidth] = { ... };

    then it compiles, but seems to be just a local variable in the constructor
    because I get a warning that it is declared but not used, and I also get
    linker errors.

    Thanks
    Victor Hannak, Sep 26, 2003
    #4
  5. Victor Hannak wrote:
    > I'm still having problems with this...
    >
    > "Kevin Goodsell" <> wrote in message
    > news:bCRab.10711$...
    >
    >>*** in the header file:
    >>
    >>class SomeClass
    >>{
    >> static const short ConstantVectorWidth = 10; /* won't work on some
    >>compilers */
    >> static const float ConstantVector[];
    >>}
    >>

    >
    >
    > Do these statements need to be in the public portion of the class
    > declaration? There is no public: or private: tags above, does that imply
    > public?


    No, it implies private for a class. It doesn't matter where you put
    them. That's determined by how you intend for the class to be used.

    >
    >
    >>*** in the implementation file:
    >>
    >>// if initialization inside the class doesn't work, do it here:
    >>// const short SomeClass::ConstantVectorWidth = 10;
    >>const float SomeClass::ConstantVector[SomeClass::ConstantVectorWidth] =
    >>{ ... };
    >>

    >
    >
    > This causes the error "Identifier 'ConstantArray' cannot have a type
    > qualifier". If I take out the type qualifier, i.e.


    'ConstantArray'? What's that?

    >
    > const float ConstantVector[SomeClass::ConstantVectorWidth] = { ... };


    This will not do what you want. It will create an array that is not a
    member of your class.

    >
    > then it compiles, but seems to be just a local variable in the constructor


    Woah, in the constructor? This should not be in the body of any
    function. It should appear at file scope.

    > because I get a warning that it is declared but not used, and I also get
    > linker errors.


    Linker errors would make sense. The compiler thinks there should be an
    array somewhere, but it doesn't exist in any compilation unit, so the
    linker can't locate it.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Sep 26, 2003
    #5
  6. > Woah, in the constructor? This should not be in the body of any
    > function. It should appear at file scope.
    >


    This is the part I was missing. Thanks.
    Victor Hannak, Sep 26, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Cheng
    Replies:
    1
    Views:
    8,171
    Juan T. Llibre
    Feb 24, 2006
  2. Alexander Farber
    Replies:
    0
    Views:
    443
    Alexander Farber
    Jun 21, 2005
  3. Replies:
    1
    Views:
    636
    Jules
    Aug 18, 2005
  4. Javier
    Replies:
    2
    Views:
    533
    James Kanze
    Sep 4, 2007
  5. news.aon.at
    Replies:
    11
    Views:
    625
    Ian Collins
    Jan 29, 2011
Loading...

Share This Page