Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass an array to a constructor?

Tags:

I want to pass an array to a constructor, but only the first value is passed--the rest looks like garbage.

Here's a simplified version of what I'm working on:

#include <iostream>

class board
{
    public:
        int state[64];
        board(int arr[])
        {
            *state = *arr;
        }
        void print();
};

void board::print()
{
    for (int y=0; y<8; y++)
    {
        for (int x=0; x<8; x++)
            std::cout << state[x + y*8] << " ";
        std::cout << "\n";
    }
}

int main()
{
    int test[64] = {
        0, 1, 2, 3, 4, 5, 6, 7,
        1, 2, 3, 4, 5, 6, 7, 8,
        2, 3, 4, 5, 6, 7, 8, 9,
        3, 4, 5, 6, 7, 8, 9,10,
        4, 5, 6, 7, 8, 9,10,11,
        5, 6, 7, 8, 9,10,11,12,
        6, 7, 8, 9,10,11,12,13,
        7, 8, 9,10,11,12,13,14 };

    board b(test);
    b.print();

    std::cin.get();
    return 0;
}

Can someone explain why this doesn't work and how to properly pass an array? Also, I don't want to copy the array. (And do I really have to indent every line by 4 spaces for code? That's pretty tedious.)

like image 669
Svad Histhana Avatar asked Feb 24 '12 07:02

Svad Histhana


People also ask

Can we pass array in constructor?

Arrays can be created using a constructor with a single number parameter.

How do you pass an array value into a constructor?

[Java] How to pass int[] array in an object constructor. //Number. java public class Number { private int[] Numbers = new int[3]; public Number(int[] array) { this. Numbers = array; } } //Setter.

Can you pass an array to a constructor in C++?

In C++, we can pass arrays as an argument to a function. And, also we can return arrays from a function. Before you learn about passing arrays as a function argument, make sure you know about C++ Arrays and C++ Functions.

Can we pass array to constructor in Java?

To pass an array to a constructor we need to pass in the array variable to the constructor while creating an object.


1 Answers

In this case it might be best to use a reference to the array:

class board
{
    int (&state)[64];

public:
    board(int (&arr)[64]) 
        : state(arr)
    {}

    // initialize use a pointer to an array
    board(int (*p)[64]) 
        : state(*p)
    {}


    void print();
};

A couple of advantages - no copying of the array, and the compiler will enforce that the correct size array is passed in.

The drawbacks are that the array you initialize the board object with needs to live at least as long as the object and any changes made to the array outside of the object are 'reflected' into the object's state. but those drawbacks occur if you use a pointer to the original array as well (basically, only copying the array will eliminate those drawbacks).

One additional drawback is that you can't create the object using a pointer to an array element (which is what array function parameters 'decay' to if the array size isn't provided in the parameter's declaration). For example, if the array is passed through a function parameter that's really a pointer, and you want that function to be able to create a board object referring to that array.

like image 125
Michael Burr Avatar answered Jan 27 '23 01:01

Michael Burr