2.2.7.1 Array


Name

Array

Remark

The OriginC Array class is a collection of nearly all data types and objects. If Array::IsOwner is TRUE, the array to be owner of the objects memory, the objects will be deleted by the array on resize or during destruction.

Hierarchy

  • Array

Examples

EX1

#include <Array.h>

class TestBase 
{
public:
    TestBase()
    {
        //printf("TestBase Constructor\n");
    }
    ~TestBase()
    {
        //printf("TestBase Destructor\n");
    }
    virtual void DoSomething()
    {
        out_str("Base class DoSomething, it should not come here if derived class had this implemented");
    }
protected:
    string m_strName;
};

class TestA : public TestBase
{
public:
    TestA(int n){
        m_strName.Format("Class TestA (%d)", n); 
        printf("constructor %s\n", m_strName);
    }
    ~TestA(){
        printf("Deconstructor %s\n", m_strName);
    }
    //virtual    
    void DoSomething(){ printf("%s:Doing my Class A thing.\n", m_strName);}
};

class TestB : public TestBase
{
public:
    TestB(int n){
        m_strName.Format("Class TestB (%d)", n); 
        printf("constructor %s\n", m_strName);
    }
    ~TestB(){
        printf("Deconstructor %s\n", m_strName);
    }
    //virtual            
    void DoSomething(){ printf("%s:Doing my Class B thing.\n", m_strName);}
};
    
void ttt()
{
    Array<TestBase&>    myList;
    myList.SetAsOwner(true);// we don't have to explicitely delete if we tell the Array to own these pointers
    
    for(int ii = 0; ii < 10; ii++)
    {
        TestBase* pMyClass;
        if(rnd() > 0.5)
            pMyClass = new TestA(ii);
        else
            pMyClass = new TestB(ii);
        
        myList.Add(*pMyClass);
    }
    
    for(int jj = 0; jj < myList.GetSize(); jj++)
    {
        TestBase& MyClass = myList.GetAt(jj);
        MyClass.DoSomething();
    }
}

Header to Include

Array.h

Reference

Members

Name Brief Example
Add Adds one element to the end of the array Examples
Array Default constructor to create an empty array Examples
GetAt Provides access to an element in the array Examples
GetSize Provides access to the size of the array Examples
InsertAt Inserts one element at a specified index in the array. Examples
IsOwner Access to the ownership value of the array Examples
RemoveAt It removes the object at the supplied index from the array. If owner, the object will be destroyed. Examples
SetAsOwner Sets the array to be owner of the Objects memory, when set the object's will be deleted by the array on resize and during destruction Examples
SetAt Adds a reference to the passed object newElement, at the indicated index nIndex. Examples
SetAtGrow Adds a reference to the passed object newElement, at the indicated index nIndex. Enlarges the array if needed. Examples
SetSize Updates array size to the requested size Examples