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

Q How to initialize CStringArray in class definition

968 views
Skip to first unread message

Bill Pate

unread,
Dec 20, 1997, 3:00:00 AM12/20/97
to

Is it possible to initialize a CStringArray in the definition of a class?
Here's an example of what I want to do:

class MyClass {
public:
CStringArray numbers[10] =
{"-178","8","382","-262","34","8374","-16","736","54","124"};
//...
};

The compiler for VC++ doesn't like it. In fact it doesn't like array
initialization at all. Here's an example for a built-in type that VC++
doesn't like:

class MyClass {
public:
double nums[3] = {3.4,5.2,8.9};
//...
};

I don't seem to find an answer to this question in any of my C++ books
[Stroustrup, Kruglinski, Prosise, Meyers,etc.]. It would be very convenient
to do it this way. If it can't be done this way, what other ways are there?

Any help would be appreciated.

Regards,
Bill Pate

Bluephi2

unread,
Dec 21, 1997, 3:00:00 AM12/21/97
to

Try initializing your array in the Class constructor instead of the definition.

class MyClass {
public:
CStringArray numbers[10];
//...
};

MyClass::MyClass()
{
numbers = {"-178","8","382","-262","34","8374","-16","736","54","124"};
}

Vedprakash

unread,
Dec 21, 1997, 3:00:00 AM12/21/97
to

Bill Pate,
Problem here is you are trying to allocate memory in class, class is
blueprint of object. In other words Class is user defined data type, and you
are expecting some preassigned value from data type. No memory is allocated
unless object of class is created.And you can not assign value to an
object/variable, which is not allocated. In my knowledge you can not do it.
Alternative is define CStringArray numbers[10]; in class and in constructor
you can assign value to each individual element( numbers[0] = "-178";
numbers[1] = "8"; .......).

vedprakash


Bill Pate wrote in message ...


>Is it possible to initialize a CStringArray in the definition of a class?
>Here's an example of what I want to do:
>

>class MyClass {
>public:
> CStringArray numbers[10] =


>{"-178","8","382","-262","34","8374","-16","736","54","124"};

0 new messages