Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Const Class Member Problem

10 views
Skip to first unread message

Jawed Karim

unread,
Feb 22, 1998, 3:00:00 AM2/22/98
to

I'm having a problem with constant class member variables. Let's say I
have a class where I:

private:
const int variable;

ANSI C++ disallows(!) the initialization of the variable in the class
definition. So I *CANNOT* do this:

const int variable = 0;

Not a problem, I do it in the constructor like this:

Class::Constructor : variable (0)
{.....}

OK, so that works. Now, what if I have a constant array? For the same
reason as above I cannot do:

const int array[3] = {1, 2, 3};

Because I cannot initialize const members in my class definition. When I
try to compile this it tells me that ANSI C++ does not allow that.
So what do I do? I cannot do:

Class::Constructor : array ( {1,2,3} )

and I also cannot do

Class:Constructor : array[0] (1), array[1] (2), array[3] (3)


What can I do? I'm stuck. Thanks, please reply to my email address because
I might miss a reply on the newsgroup otherwise.

jka...@students.uiuc.edu


Les Matheson

unread,
Feb 22, 1998, 3:00:00 AM2/22/98
to

Since your array is const, you can make it static also, and then you
can initialize it just like any static array:

class Test
{
public:
static const int array[3];
};

const int Test::array[3] = { 1, 2, 3 };

Les
*** Reply email address: remove 'nospam-'... (Don't you just hate that
stuff?)


Jawed Karim wrote in message ...

James L. Preston

unread,
Feb 23, 1998, 3:00:00 AM2/23/98
to


Jawed Karim wrote in message ...
>I'm having a problem with constant class member variables. Let's say I
>have a class where I:
>
>private:
> const int variable;
>
>ANSI C++ disallows(!) the initialization of the variable in the class
>definition. So I *CANNOT* do this:
>
> const int variable = 0;
>
>Not a problem, I do it in the constructor like this:
>
>Class::Constructor : variable (0)
>{.....}
>
>OK, so that works. Now, what if I have a constant array? For the same
>reason as above I cannot do:
>
> const int array[3] = {1, 2, 3};
>
>Because I cannot initialize const members in my class definition. When I
>try to compile this it tells me that ANSI C++ does not allow that.
>So what do I do? I cannot do:
>
>Class::Constructor : array ( {1,2,3} )
>
>and I also cannot do
>
>Class:Constructor : array[0] (1), array[1] (2), array[3] (3)
>

And Les Matheson replied:

>Since your array is const, you can make it static also, and then you
>can initialize it just like any static array:

>class Test
>{
> public:
> static const int array[3];
>};

>const int Test::array[3] = { 1, 2, 3 };

But they are not the same thing, because a const data member
is not necessarily the same value across all instances of the
class, the way a static data member is.

There are a number of other alternatives, but the one
that comes to mind easily is to use the STL, where appropriate
constructors have been defined, unlike for plain-old arrays,
so you can do something like:
#include <iostream.h>
#include <vector.h>
class Test{
public:
Test(const vector<int> newArray):array(newArray){}
// stupid test function
const int getVal(int index) const {return array[index];}
private:
const vector<int> array;
};

main(int argc, char* argv[])
{
vector<int> initializationData;
for (int ii=0; ii<5; ii++)
initializationData.push_back(ii);
Test myTest(initializationData);
for (int ii=0; ii<5;ii++)
cout << "val " << ii<< " = " << myTest.getVal(ii) << endl;
}

And the constant data is instance-specific, if that is what was desired.

later,
jlp

0 new messages