0

I have a simple class representing a triangle, which contains three arrays.

class Triangle
{
public:
  double         X[3];
  double         Y[3];
  unsigned char color[3];
};

I want to create objects of this class on the heap, then pass them to a function that will use values from the arrays. Since I am passing in these objects, I need a copy constructor to make a deep copy. The problem arises when allocating the new arrays in the copy constructor.

This is what I have thus far:

Triangle (const Triangle &obj)
{
    X = new double[3];
    Y = new double[3];
    color = new unsigned char[3];
    for (int i=0; i<3; ++i)
    {
        X[i] = obj.X[i];
        Y[i] = obj.Y[i];
        color[i] = obj.color[i];
    }
}

I keep getting the following error: " error: array type 'double [3]' is not assignable" for each of the three arrays.

I am taking the same approach as discussed in this video, and I cannot figure out why I am unable to make a new array. The answer to this question also has the same approach.

Does anyone have any insights? I feel like I am just missing something really silly.

Community
  • 1
  • 1
N-C
  • 155
  • 1
  • 9

3 Answers3

1

I am taking the same approach as discussed in this video.

In the video you can see that he used the member-initializer list to initialize the array. Read up on the member-initializer list and how it differs from assigning data members inside the constructor body.

You can also see that he used a pointer and dynamically allocated with new. This is needed (sic) if the size of the array isn't known at compile time. In your case it is known to be 3, hence no need for new.

Once you've declared the array, you have the array. There's no need to allocate new memory and copy things over since the space has already been allocated for the array. As @WhozCraig said, you don't even need a copy-constructor since one will be implicitly generated that does what you need anyway.

The answer to this question also has the same approach.

Simple, C++ is not Java.

David G
  • 94,763
  • 41
  • 167
  • 253
0

Its because 'X', 'Y', and 'color' are not pointer types. Even without deep copy, those arrays will be allocated and will have the same values.

0

The reason new arrays cannot be allocated in the Triangle constructor is that the X and Y fields are not pointers to double, they are arrays of type double.

There is a lot of confusion about the difference between arrays and pointers in C and C++. In this case, when a new Triangle is created, memory is allocated that will be large enough hold 6 doubles and 3 unsigned chars, and the compiler will base the addresses of X, Y and color on the starting address of the memory allocated for the entire class.

Community
  • 1
  • 1
Jay Elston
  • 1,978
  • 1
  • 19
  • 38
  • Oh, I see. I think that makes sense, but things still aren't working correctly. I create an array of Triangles with 'new', then pass references to each triangle to a function that initializes the arrays within each Triangle. When the initializer function returns, it acts like I passed the triangles by value, as no Triangle values change outside that function. Do arrays of Triangles act differently, even though I pass &triangleArray[0] to the function? – N-C Oct 11 '14 at 04:48