0

So I wrote this C++ Class for storing an Array:

#include<bits/stdc++.h>
using namespace std;

class Array
{
    private:
        int size;
        int *A = new int[size];
        int length;
    public:
        Array(int arr[],int sz = 10)
        {
            length = sz;
            size = 10;
            for(int i=0;i<length;i++)
                A[i] = arr[i];
        }
        ~Array()
        {
            delete []A;
        }
    void Display();
    void Insert(int index, int x);
};

void Array::Display()
{//code}

void Array::Insert(int index, int x)
{//code}

int main()
{
    int a[3] = {1,2,3};
    Array arr(a, 3);
    arr.Display();
    arr.Insert(1,4);
    arr.Display();
}   

This works fine. But in the main() function:

int main()
{
    int a[3] = {1,2,3};
    Array arr(a, 3);
    arr.Display();
} 

I am passing the pointer to the array a in the Class constructor. Is there a way to pass the array like this?

Array arr({1,2,3}, 3);
  • 4
    Maybe you want to have a constructor taking a `std::initializer_list` as parameter? – UnholySheep Apr 14 '20 at 16:28
  • 1
    UnholySheep's comment is the correct solution. And unrelated, but your allocation isn't doing what you probably think it's doing. – Stephen Newell Apr 14 '20 at 16:30
  • 3
    Warning: at the time of `int *A = new int[size];`, `size` has yet to be initialized. You will need to use a [member Initializer List](https://en.cppreference.com/w/cpp/language/initializer_list). – user4581301 Apr 14 '20 at 16:30
  • Fun fact: `std::initializer_list` knows how big it is, so you won't need to pass in the length of the array. – user4581301 Apr 14 '20 at 16:31
  • @user4581301 thanks for pointing that out, I didn't know that. :) – Aviral Rana Apr 14 '20 at 16:35
  • 1
    "This works fine" no it doesn't. Try to copy your object. Read on [the rule of three](https://en.cppreference.com/w/cpp/language/rule_of_three) – bolov Apr 14 '20 at 16:36
  • 2
    Warning: Be wary of `#include`. [For many reasons](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h), you should prefer to use the correct library headers. Coupling bits/stdc++.h with [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) greatly increases the risks inherent in both. You have effectively included the tens of thousands of identifiers in the C++ Standard Library and pulled them all into the global namespace where they can very easily interfere with your identifiers. – user4581301 Apr 14 '20 at 16:38
  • 1
    Consider using `std::array` or `std::vector` instead of C-style arrays. – Jesper Juhl Apr 14 '20 at 16:42

1 Answers1

0

You probably need to use something like this. But please note this code is still not optimal and it is usually much better to use std::vector or in some cases std::array.

#include <initializer_list>    

class Array
{
private:
    int *m_data = nullptr;
    int m_size = 0;
    int m_capacity = 0;   
public:
    Array(int arr[], int sz = 10) {
        m_size = sz;
        m_capacity = sz;
        m_data = new int[m_capacity];
        for(int i=0; i < m_size; i++) {
            m_data[i] = arr[i];
        }
    }
    Array(const std::initializer_list<int> &il) {
        m_size = il.size();
        m_capacity = il.size();  
        m_data = new int[m_capacity];
        auto it = il.begin();
        for (int i = 0; i < m_size; ++i) {
            m_data[i] = *it;
            ++it;
        }
    }
    ~Array() {
        delete [] m_data;
    }

    int size() const { return m_size; }
    bool empty() const { return m_size == 0; }

    int& operator[](int id) { return  m_data[id]; }
    int operator[](int id) const { return  m_data[id]; }
};



#include <cstdio> // for printf

int main() {
    Array arr({1,2,3});

    printf(" %i \n",arr.size());
    return 0;
}
DevO
  • 365
  • 1
  • 8