Changeset 1285


Ignore:
Timestamp:
Apr 24, 2012, 1:07:35 AM (11 years ago)
Author:
sam
Message:

core: add copy constructor and assignment operator to Array.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/array.h

    r1269 r1285  
    3131    inline ~Array() { delete[] m_data; }
    3232
     33    Array(Array const& that) : m_data(0), m_count(0), m_reserved(0)
     34    {
     35        Reserve(that.m_reserved);
     36        memcpy(m_data, that.m_data, m_count * sizeof(Element));
     37        m_count = that.m_count;
     38    }
     39
     40    Array& operator=(Array const& that)
     41    {
     42        m_data = 0;
     43        m_count = 0;
     44        m_reserved = 0;
     45        Reserve(that.m_reserved);
     46        memcpy(m_data, that.m_data, that.m_count * sizeof(Element));
     47        m_count = that.m_count;
     48        return *this;
     49    }
     50
    3351    inline Element& operator[](int n)
    3452    {
     
    118136    inline ~Array() { delete[] m_data; }
    119137
     138    Array(Array const& that) : m_data(0), m_count(0), m_reserved(0)
     139    {
     140        Reserve(that.m_reserved);
     141        memcpy(m_data, that.m_data, m_count * sizeof(Element));
     142        m_count = that.m_count;
     143    }
     144
     145    Array& operator=(Array const& that)
     146    {
     147        m_data = 0;
     148        m_count = 0;
     149        m_reserved = 0;
     150        Reserve(that.m_reserved);
     151        memcpy(m_data, that.m_data, that.m_count * sizeof(Element));
     152        m_count = that.m_count;
     153        return *this;
     154    }
     155
    120156    inline Element& operator[](int n)
    121157    {
     
    198234{
    199235public:
     236    typedef T1 Element;
     237
    200238    inline Array() : m_data(0), m_count(0), m_reserved(0) {}
    201239    inline ~Array() { delete[] m_data; }
    202240
    203     inline T1& operator[](int n)
    204     {
    205         return m_data[n];
    206     }
    207 
    208     inline T1 const& operator[](int n) const
     241    Array(Array const& that) : m_data(0), m_count(0), m_reserved(0)
     242    {
     243        Reserve(that.m_reserved);
     244        memcpy(m_data, that.m_data, m_count * sizeof(Element));
     245        m_count = that.m_count;
     246    }
     247
     248    Array& operator=(Array const& that)
     249    {
     250        m_data = 0;
     251        m_count = 0;
     252        m_reserved = 0;
     253        Reserve(that.m_reserved);
     254        memcpy(m_data, that.m_data, that.m_count * sizeof(Element));
     255        m_count = that.m_count;
     256        return *this;
     257    }
     258
     259    inline Element& operator[](int n)
     260    {
     261        return m_data[n];
     262    }
     263
     264    inline Element const& operator[](int n) const
    209265    {
    210266        return m_data[n];
     
    248304            return;
    249305
    250         T1 *tmp = new T1[count];
     306        Element *tmp = new Element[count];
    251307        if (m_data)
    252308        {
    253             memcpy(tmp, m_data, m_count * sizeof(T1));
     309            memcpy(tmp, m_data, m_count * sizeof(Element));
    254310            delete[] m_data;
    255311        }
     
    259315
    260316    inline int Count() const { return m_count; }
    261     inline int Bytes() const { return m_count * sizeof(T1); }
     317    inline int Bytes() const { return m_count * sizeof(Element); }
    262318
    263319private:
    264     T1 *m_data;
     320    Element *m_data;
    265321    int m_count, m_reserved;
    266322};
Note: See TracChangeset for help on using the changeset viewer.