how use array inicializations on constructor?

see these constructor:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template<typename TypeID>
class array2
{
private:
    TypeID *arr=NULL;
    int intSize=0;
public:
    array2(unsigned int uintSize, TypeID *elements)
    {
        if(arr!=NULL)
            free(arr);
        arr =(TypeID*)realloc( arr, uintSize * sizeof (*arr) );
        intSize=uintSize;
        for(unsigned int uintIndex=0;uintIndex<uintSize; uintIndex++ )
        {
            arr[uintIndex]=elements[uintIndex];
        }
    }
//theres more....
};

//how i use it:
int inttest[]={2,5,7,8,9};
array2<int> arr(5,inttest);


but how can i validate these test:
array2<int> arr(5,{2,5,7,8,9});
???
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <initializer_list>

template<typename T>
class array2 {
private:
    T *arr;
    size_t size;

public:
    array2(size_t sz, T *elements)
        : arr{new T[sz]}, size{sz}
    {
        for (size_t i = 0; i < sz; i++)
            arr[i] = elements[i];
    }

    array2(std::initializer_list<T> lst)
        : arr{new T[lst.size()]}, size{lst.size()}
    {
        T *a = arr;
        for (const auto& x: lst)
            *a++ = x;
    }

    ~array2()
    {
        delete [] arr;
    }

    void print()
    {
        for (size_t i = 0; i < size; i++)
            std::cout << arr[i] << ' ';
        std::cout << '\n';
    }
};

int main() {
    array2<int> arr({2, 5, 7, 8, 9});
    arr.print();
}

Last edited on
understood... you used the std::initializer_list<T> lst parameter for valid that method. thank you so much
I also got rid of your use of realloc (a C function) in favor of new (a C++ operator) and removed the part that tries to free the array if arr isn't NULL (we actually say nullptr in C++). That is not necessary since the object is just now being constructed so it can't have any members. In fact, arr would have an arbitrary value at that point, so the free() may in fact be called with an arbitrary value. That's a problem!

I just now went back and added a destructor since the allocated memory needs to be deleted when you are done with it. I also changed unsigned to size_t (which is more appropriate) and used member initialization lists in the constructors.
Last edited on
speaking on that realloc() used times must be igual free() used times?
realloc();
free() - realloc();
free() - realloc();
free() - realloc();
end:
free()

????

don't forget: using the 'new'(yes i must 'delete[]'), we lose the previous values
Last edited on
Does free need to be used an equal number of times as realloc?

Absolutely not! Realloc "reallocates" an already existing array. Since it doesn't create a new one, even if you used realloc 100 times you would only need to call free once.

But that is C, not C++. You need to make up your mind which language you are using. In C++, if we want to reallocate an array, we create a new one first, copy the old one to the new one, then delete the old one.

Don't forget: using the 'new'(yes i must 'delete[]'), we lose the previous values

You totally missed my point. THERE ARE NO PREVIOUS VALUES. How could there be? The constructor is where the array is created.
Last edited on
some arrays can be resized without losing the previous values.
ok.. for that we have 'insert'(or similar). and true theres STATIC and DYNAMIC arrays.
thank you so much for all
some arrays can be resized without losing the previous values.

You simply don't know what you are talking about, buddy.
I'm done with this.
because i used, before, Visual Basic ;)
closed account (E0p9LyTq)
C++ is not Visual Basic. You have to unlearn what you learned in VB.
Topic archived. No new replies allowed.