1

I need to pass a pointer to an array in a constructor (I am aware that using plain arrays in C++ is considered by many as bad practice, but for now I just want to go ahead with it).

Please consider the following code:

// let's omit includes

class A {
   // irrelevant
}

class B {
    public:
    //irrelevant
    void someMethod() {
        _c = new C(array_id);
    }

    private:
    C* _c;
    A* array_id[SOME_CONST];
}

class C {
    public:
    C(A* a_array) : _array(a_array) {}
    private:
    A* _array;
}

Trying to compile will result in a:

error: no matching function for call to C::C(A* [SOME_CONST])'

As far as I know, the identifier of an array decays to a pointer to the first element of the array, but it seems that it cannot be used directly. How can I effectively pass a pointer to an array of A* in the constructor of C?

7
  • 4
    It looks like you are passing an array of pointers where a pointer is expected. Commented Feb 28, 2015 at 10:08
  • 2
    Right, the identifier for the array decays into a pointer to the first element of the array. The first element of the array is an A*, so a pointer to that is an A**, and you don't have a constructor that accepts an A**. Commented Feb 28, 2015 at 10:13
  • @immibis Why would the identifier for the array decay here? There's nothing it can decay to. Commented Feb 28, 2015 at 10:20
  • 1
    @roymcclure Shouldn't this A* array_id[SOME_CONST]; be A array_id[SOME_CONST];? Commented Feb 28, 2015 at 10:22
  • @juanchopanza Why do you think it can't decay to a pointer to its first element? Commented Feb 28, 2015 at 10:45

3 Answers 3

2

According to the C++ Standard

4.2 Array-to-pointer conversion [conv.array]
An lvalue or rvalue of type “array of N T” or “array of unknown bound of T” can be converted to a prvalue of type “pointer to T”. The result is a pointer to the first element of the array.

Modify your code like this:

   class C {

        public:
        typedef A* PTA[SOME_CONST];
        C(PTA a_array) : _array(a_array) {}
        private:
        A** _array;//Note this line,type decays.
    };
Sign up to request clarification or add additional context in comments.

Comments

1

You have two problems: The first is that you're calling the C constructor with an array of pointers, not an array. The second problem is that you're trying to assign that allocation to an incompatible type.

Solution might be something like

class B
{
public:
    void someMethod()
    {
        my_c = new C(my_a_array);
    }

private:
    A my_a_array[SOME_CONST];
    C* my_c;
};

That will allocate a single object of type C, passing the array of A objects to it.

Comments

1

How can I effectively pass a pointer to an array of A* in the constructor of C?

C(array_id);

The problem is that the C constructor doesn't accept an array of A*, it accepts an A*.

If you want to pass an array of A* to the C constructor then you need to declare it to take an A**:

C(A** a_array)

Or, equivalently:

C(A* a_array[])

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.