News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

C++ array tips - Dimensions and initialization of arrays

Started by ben2ong2, October 06, 2006, 04:53:28 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

ben2ong2

PROBLEM: You are not allowed to view links. Register or Login (Stefan Schwarz)

I want to use a static array within a class, and therefore i
initialize it as follows:

foo.h:
------

class Foo
{
private:
        static int array[2][2];
public:
        Foo() {};
};

foo.c:
------
int Foo::array[2][2] = { 1,2, 3,4};

Is there any way to avoid the double definition of the array-bounds, so
i could easyly change the size of my array.


RESPONSE: p j l @sunb4.cs.uiuc.edu (Paul Lucas), 14 Jul 92

First, for a 2d array, you need only specify the number of columns.
Now then:

   class Foo {
      enum { Cols = 2 };
      static in array[][Cols];
      // ...
   public:
      // ...
   };

   int Foo:array[][Cols] = { 1,2,3,4 };

To change the number of colums, just change Foo::Cols.


You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login