0

I have a problem. I want to initilize my 2Dim array ,which is a member of a class,in the constructor of a class. For example .

class Foo
{
    private:

       bool testArray[100][4];

    public:

       Foo( bool t_array[][4]);         
};

in a Foo.cpp file :

Foo::Foo(bool array[][4])
{
   // initilize it in there with unknown row size 
}

I dont have a specific row size but I know that it will be maximum 100.

How could I initilize my testArray in my constructor? thanks in advance.

Bambi
  • 33
  • 1
  • 6

1 Answers1

1

Since your array of type bool [100][4] will decay into pointer of type bool (*) [4] when passed to the constructor, you should pass the count of rows as well. There's no other way how this constructor would find out the real size of this array.

Alternatively you might consider using std::vector< std::array<bool, 4> > instead.

LihO
  • 41,190
  • 11
  • 99
  • 167
  • thanks for reply. I dont have any right to change types of functions. Is there a way to figure it out without change the declarations ? – Bambi Oct 12 '13 at 10:40
  • @Bambi: Maybe it would be possible if you used some sequence of values to mark the end of the array. However that doesn't seem to be much of a reasonable solution. Why aren't you able to change the declaration? – LihO Oct 12 '13 at 10:43
  • @Bambi You been asked to do something impossible, or you've misunderstood what you are expected to do. – john Oct 12 '13 at 11:00