Solved

Easy - Declaring a 2d array in a class.

Posted on 2007-03-22
9
192 Views
Last Modified: 2010-04-01
Hi,

class myClass{
     private:
          int testData[][]={
               {9,4,7},
               {7,9,4},
               {4,7,9};
          };
};

This is apparently missing a subscript (but if I declare the contents of the array when the variable is declared I don't have to specify the size?). With the sizes put in there is an error pointing to '{' on the line where the variable is declared. Anybody?

Thanks,
Uni
0
Comment
Question by:Unimatrix_001
  • 4
  • 3
  • 2
9 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 250 total points
ID: 18770201
When you make it static, you can do it like this :

class myClass{
     private:
          static int testData[3][3];
};

int myClass::testData[3][3] = {
                                {9,4,7},
                                {7,9,4},
                                {4,7,9}
                              };
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18770203
Also note that you had an extra ; in your initialization data ...
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18770210
Don't suppose it's doable without making it static?
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 250 total points
ID: 18770218
You can't initialize data members in a class. You need to do initialization in the constructor most preferable in the initializer list but there is no way to init an array in the initializer list. Also in the body you only can init the whole array by assigning another array:
class myClass{
     private:
          int testData[3][3];  // required to determine class size
     public:
            myClass()
            {
               static int defData[][3] ={
               {9,4,7},
               {7,9,4},
               {4,7,9},
               };
               testData = defData;
           }
};

Regards, Alex


};
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18770237
Woa... screw that idea then... static it is. Points split. :-)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18770252
>> Woa... screw that idea then... static it is.
That will only work if your data CAN be static ... ie. if it doesn't change from object instance to object instance.
Otherwise, you'd have to initialize the data in the constructor as Alex explained.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18770265
Fortunately it can be... I'm just not mad keen on having some static element in a class if I can really help it, but in this case static just seems a neater way of doing it. :)
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18770322
Note, the static array within my constructor not necessarily need to be statci. However as it used as const array it gives less overhead when making it static.
Instead of defining the default array statically in the (one) constructor you may combine both methods and define the default array as a static class member.

class myClass{
     private:
          int testData[3][3];  // required to determine class size
          static int defData[3][3];
     public:
            myClass()
            {
               static int defData[][3] ={
               {9,4,7},
               {7,9,4},
               {4,7,9},
               };
               testData = defData;
           }
};

// myclass.cpp
#include "myclass.h"

int MyClass::defData[3][3] ={
               {9,4,7},
               {7,9,4},
               {4,7,9},
               };

>>>> Woa... screw that idea

In praxis fixed arrays containing const data are very common. And it makes absolutely sense to makes them static class members cause there is no need to have a copy with any new instance of the class but to have it only once. So, the inconvenience that you can set all array data only at initialization time and not for not-static members isn't so much a problem in real-life programming but more for test or educational purposes.

Regards, Alex
   
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 18770431
@Alex: Thanks for the further info... :) It looks quite an interesting idea, didn't think of anything like that!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now