const array declaration/init for a class

V

Victor Hannak

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

Kevin Goodsell

Victor said:
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
 
W

WW

Kevin said:
*** 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.
 
V

Victor Hannak

I'm still having problems with this...

Kevin Goodsell said:
*** 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
 
K

Kevin Goodsell

Victor said:
I'm still having problems with this...

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top