0

I am trying to initialize the dynamic array in the constructor using initialize_list in C++. How can I achieve this?

#include <cstdlib> 
#include <initializer_list>
#include <iostream>
#include <utility>

using namespace std;

class vec {
private:
    // Variable to store the number of elements contained in this vec.
    size_t elements;
    // Pointer to store the address of the dynamically allocated memory.
    double *data;

public:
    /*
      * Constructor to create a vec variable with the contents of 'ilist'.
    */
    vec(initializer_list<double> ilist);
}

int main() {
    vec x = { 1, 2, 3 };  // should call to the constructor 
    return 0;
}
Stack Danny
  • 7,754
  • 2
  • 26
  • 55
pod
  • 23
  • 7

2 Answers2

1

initializer_list has size method, it gives you information how many elements must be allocated by new, so it could be:

vec(initializer_list<double> ilist)
{
    elements = ilist.size();
    data = new double[ ilist.size() ];
    std::copy(ilist.begin(),ilist.end(),data);
}
rafix07
  • 20,001
  • 3
  • 20
  • 33
  • thanks you @rafix07, so every time filling the data array needs to be done using copy function only? as ilist is a temporary memory – pod May 29 '19 at 07:41
  • @MohanNagella better use [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) here. But if not, make sure to at least provide a destructor to `delete[]` `this->data`. – Stack Danny May 29 '19 at 07:43
  • @MohanNagella You can use `copy`, or raw loop to iterate over elements from initializer_list and copy data, it doesn't matter, but you just need to copy data from ilist into dynamic allocated data to initialize it. – rafix07 May 29 '19 at 07:44
  • @StackDanny you mean `delete[] data;` instead. – Remy Lebeau May 29 '19 at 07:47
  • yes @RemyLebeau, correct. I didn't know the bit about move operations, interesting. – Stack Danny May 29 '19 at 07:49
  • 1
    @MohanNagella if you decide to implement a manual array using `new[]`, you need to implement copy/move constructors, copy/move assignment operators, and a destructor, per the [Rule of 3/5/0](https://en.cppreference.com/w/cpp/language/rule_of_three). Using `std::vector` instead, you get that for free. – Remy Lebeau May 29 '19 at 07:49
  • @StackDanny Yeah! I forgot to mention destructor and as my purpose was using dynamic array so I am using them but vector as u said a better approach thank you – pod May 29 '19 at 07:50
  • @RemyLebeau I am going to use all the special functions in this code just to familiar my self with dynamic and overloadings – pod May 29 '19 at 07:52
  • @MohanNagella I suppose you should accept RemyLebeau answer therefore. Also you might want to get rid of `using std::namespace std;` you can read [here](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) why. – Stack Danny May 29 '19 at 07:52
1

Use a standard std::vector container instead of a raw pointer. std::vector is a wrapper for a dynamic array, and it has a constructor that accepts a std::initializer_list as input.

#include <initializer_list>
#include <iostream>
#include <vector>
using namespace std;

class vec {
private:
    vector<double> data;

public:
    vec(initializer_list<double> ilist) : data(ilist) {}
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770