0

I'm currently learning C++ and I'm messing around with constructors/operators.

I have an array:

int x(1);
int a[] = { int(x), int(x), int(x) };

How can I construct the same array dynamically using the object's copy constructor? I also don't wanna use the = operator.

So something like:

int* b;
for (int i = 0; i < DESIRED_ARRAY_SIZE; i += 1)
{
    b[i](int(x));
}

Of course the above doesn't work. This is strictly for learning purposes, so I'm wondering if this is possible. I'm also wondering if there's a special case for primitives vs classes, as I'm currently testing with int atm.

kir
  • 581
  • 1
  • 6
  • 22

4 Answers4

0

You asked:

How can I construct the same array dynamically using the object's copy constructor?

Assuming you have a different type than int that has a copy constructor, you can use:

X x(1);
X arr[] = {x, x, x, x};

You said:

I also don't wanna use the = operator.

If you have the option of using std::vector, you can use:

std::vector<X> arr(10, x);

If using std::vector is not an option for some reason, you can use:

// Create an array objects. The object will be created using
// the default constructor.
X* arr = new X[DESIRED_ARRAY_SIZE]; 
for (int i = 0; i < DESIRED_ARRAY_SIZE; i += 1)
{
    arr[i] = x; // This will use the copy assignment operator,
                // not the copy constructor.
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • I coded the above snippet that way to explicitly denote that I want to use the copy constructor for each of the array's elements. And I'd like to know if this is possible without using vectors :). – kir Feb 02 '15 at 00:24
0

In C++ dynamic arrays are called vector and (since C++11) you can write:

vector<int> a = { x, x, x };

The casts are redundant, int(x) is the same as x, because x is an int already. (That doesn't specify a copy).

Initializer lists copy by value.

In your second example the code is:

vector<int> b(DESIRED_ARRAY_SIZE, x);
M.M
  • 138,810
  • 21
  • 208
  • 365
  • Can this be done without vectors? I completely forgot to mention that in my problem :(. Oh and yes I'm aware that my code was redundant. See my comment on the other answer. – kir Feb 02 '15 at 00:25
  • "vector" is how you construct arrays using dynamically-allocated memory in C++ ; that's like asking if you can screw in a screw without using a screwdriver. You could use a hammer but results are not so good... – M.M Feb 02 '15 at 00:27
0

The = sign gets used in two contexts:

  1. Initialization - Copy, Copy-list or aggregate initialization
  2. Assignment

When you write something like int a[] = {1,2,3}, it is aggregate initialization. If you want to construct an array dynamically, you would use new[] to dynamically allocate your array and then go on to copy assign the elements of this array. You do it like this:

int* b = new int[DESIRED_ARRAY_SIZE];
for (int i = 0; i < DESIRED_ARRAY_SIZE; i += 1)
{
    b[i] = x;
}

There is a difference between using fundamental types like int and a class type, say T. In the above snippet, you can replace int by T(and have x have type T) and it will work if T is default constructible and has a copy constructor. If not, there are ways to specify how the objects of type T in b should be constructed. You can find information in the section titled "Construction" here. Note that there are much better ways to do this in modern C++, but that wasn't what your question was about.

Pradhan
  • 16,391
  • 3
  • 44
  • 59
0

As other answers have pointed out, best choice would be to use std::vector, else you can use new and delete althought its not recommended since it's easy to make mistakes. Always remember to delete[] when you've created an array using new and delete if you've just used new without making it an array. Consider looking at the other answers here about std::vector for dealing with dynamic allocations. If none of the answers are what you're looking for, then perhaps you need to rephrase your question.

    int* p_array = new int[3]; // '3' would be the array length
    for (int i{0}; i != 3; ++i)// I'm not naming the arr length in this example.
        p_array[i] = i;  

    delete[] p_array;
Andreas DM
  • 10,685
  • 6
  • 35
  • 62