Connecting Tech Pros Worldwide Forums | Help | Site Map

const array declaration/init for a class

Victor Hannak
Guest
 
Posts: n/a
#1: Jul 19 '05
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?



Kevin Goodsell
Guest
 
Posts: n/a
#2: Jul 19 '05

re: const array declaration/init for a class


Victor Hannak wrote:
[color=blue]
> 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?[/color]

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.

WW
Guest
 
Posts: n/a
#3: Jul 19 '05

re: const array declaration/init for a class


Kevin Goodsell wrote:[color=blue]
> *** 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...[/color]

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


Victor Hannak
Guest
 
Posts: n/a
#4: Jul 19 '05

re: const array declaration/init for a class


I'm still having problems with this...

"Kevin Goodsell" <usenet1.spamfree.fusion@neverbox.com> wrote in message
news:bCRab.10711$BS5.8650@newsread4.news.pas.earth link.net...[color=blue]
> *** in the header file:
>
> class SomeClass
> {
> static const short ConstantVectorWidth = 10; /* won't work on some
> compilers */
> static const float ConstantVector[];
> }
>[/color]

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?
[color=blue]
> *** 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] =
> { ... };
>[/color]

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


Kevin Goodsell
Guest
 
Posts: n/a
#5: Jul 19 '05

re: const array declaration/init for a class


Victor Hannak wrote:[color=blue]
> I'm still having problems with this...
>
> "Kevin Goodsell" <usenet1.spamfree.fusion@neverbox.com> wrote in message
> news:bCRab.10711$BS5.8650@newsread4.news.pas.earth link.net...
>[color=green]
>>*** in the header file:
>>
>>class SomeClass
>>{
>> static const short ConstantVectorWidth = 10; /* won't work on some
>>compilers */
>> static const float ConstantVector[];
>>}
>>[/color]
>
>
> 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?[/color]

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.
[color=blue]
>
>[color=green]
>>*** 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] =
>>{ ... };
>>[/color]
>
>
> This causes the error "Identifier 'ConstantArray' cannot have a type
> qualifier". If I take out the type qualifier, i.e.[/color]

'ConstantArray'? What's that?
[color=blue]
>
> const float ConstantVector[SomeClass::ConstantVectorWidth] = { ... };[/color]

This will not do what you want. It will create an array that is not a
member of your class.
[color=blue]
>
> then it compiles, but seems to be just a local variable in the constructor[/color]

Woah, in the constructor? This should not be in the body of any
function. It should appear at file scope.
[color=blue]
> because I get a warning that it is declared but not used, and I also get
> linker errors.[/color]

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.

Victor Hannak
Guest
 
Posts: n/a
#6: Jul 19 '05

re: const array declaration/init for a class


> Woah, in the constructor? This should not be in the body of any[color=blue]
> function. It should appear at file scope.
>[/color]

This is the part I was missing. Thanks.


Closed Thread