Initializing multidimensional arrays in constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Allan A.

    #1

    Initializing multidimensional arrays in constructor

    Hi all. Is there any way to initialize an entire array in the constructor.
    The code below does not work. Thanks.

    class x {
    public:
    int array[2][2];
    x();

    };

    x::x() {
    array = {{1,2},{3,4}};
    }

  • Ivan Vecerina

    #2
    Re: Initializing multidimensiona l arrays in constructor

    "Allan A." <AA@alphaville. com> wrote in message
    news:dti0gh01tl 0@news4.newsguy .com...
    : Hi all. Is there any way to initialize an entire array in the
    constructor.

    No, unfortunately, not in the current version of the C++ standard.
    [ it is being proposed to allow this in the next revision of
    the standard, but this is still years away. See for example
    http://www.open-std.org/jtc1/sc22/wg...2005/n1919.pdf ]

    : The code below does not work. Thanks.
    :
    : class x {
    : public:
    : int array[2][2];
    : x();
    :
    : };
    :
    : x::x() {
    : array = {{1,2},{3,4}};
    : }

    The actual (proposed) syntax for an initialization would
    look like:
    x::x()
    : array( {{1,2},{3,4}} )
    { }

    The example your provided uses assignment, not initialization,
    to set the contents of the array. It could become legal as
    well if(/once) the above proposal is integrated in the standard.
    Equivalent code can (and has) be written today as individual
    assignments to each element of the array.


    Ivan
    --
    http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
    Brainbench MVP for C++ <> http://www.brainbench.com


    Comment

    • Jay_Nabonne

      #3
      Re: Initializing multidimensiona l arrays in constructor

      On Wed, 22 Feb 2006 15:37:53 +0000, Allan A. wrote:
      [color=blue]
      > Hi all. Is there any way to initialize an entire array in the constructor.
      > The code below does not work. Thanks.
      >
      > class x {
      > public:
      > int array[2][2];
      > x();
      >
      > };
      >
      > x::x() {
      > array = {{1,2},{3,4}};
      > }[/color]

      You could do this:

      x::x() {
      static int initData[2][2] = {{1,2},{3,4}};
      memcpy(array, initData, sizeof(array));
      }

      - Jay

      Comment

      • mlimber

        #4
        Re: Initializing multidimensiona l arrays in constructor

        Allan A. wrote:[color=blue]
        > Hi all. Is there any way to initialize an entire array in the constructor.
        > The code below does not work. Thanks.
        >
        > class x {
        > public:
        > int array[2][2];
        > x();
        >
        > };
        >
        > x::x() {
        > array = {{1,2},{3,4}};
        > }[/color]

        You could use vector< vector<int> > and do something similar to this:



        Then the member could even be const if desired.

        Cheers! --M

        Comment

        Working...