Next Previous Contents

3. Usage

3.1 General Usage

To compile a C++ program with this library you must add the switch

-I /usr/src/classlib
in your C++ command line and the
-lclasslib
in your linker command line.

The classlib is a shared library and can be included and shipped in your program in binary format.

3.2 Library at work

This section cover the most common usage of the library.

The container classes

The container classes rapresent the major part of the library. A large types of container can cover all the normal request for code writing.

Every object used in classlib must have at least an operator== for basic container opertion. The Find() function use this operator for comparision.

The sorted variant of the container (TSArray a sorted array and so on) also require a operator < to make a correct order for object items.

Generally for every type of container two forms are available: direct and indirect container.

Direct container

Direct containers holds object of the declared type. It means the object must have a copy semantics (operator=) to put instance in the container:

...
// it work fine: the base types have all operator= and operator==
// the array will be created with initial size of 100 doubles, the base index is 0,
// the step increment when add more than 100 elements will be 10
TArray<double> arDbl(100, 0, 10);
arDbl.add(123.45);
...
...
class myClass
{
public:
        int var1, var2, var3;
        myClass& operator=(const myClass& c) {
                var1 = c.var1;
                var2 = c.var2;
                var3 = c.var3;
                return *this;
        }

        int operator==(const myClass& c) const { return  == this; }
};
...
...
TArray<myClass> arMC(10, 0, 10);

myClass c1;
c1.var1 = 10;
c1.var2 = 20;
c1.var3 = 30;
...
// insert the instance (not the address) in the array.
// the entry in array will be copied from c1 with the operator=
arMC.add(c1);
...
...

Indirect container

Indirect containers holds pointers to object of the declared type. A pointer have always a copy semantics; a operator= is not necessary in this case.

...
class myClass
{
public:
        int var1, var2, var3;

        int operator==(const myClass& c) const { return  == this; }
};
...
...
TIArray<myClass> arMC(10, 0, 10);

myClass c1 = new myClass;
c1->var1 = 10;
c1->var2 = 20;
c1->var3 = 30;
...
// insert the pointer in the array.
// DON'T CALL delete ON THE c1 POINTER
arMC.add(c1);
...
...

Every indirect container support the concept of ownership of the object put into them. If ownership is on (the default) when the container will be destroied it destroy also the contined objects. The default behavory can be changed with the

OwnsElements(int own)
method. If 'own' is zero the ownership will be turned off, otherwise turned on.


Next Previous Contents