## CGAL::Object

#include <CGAL/Object.h>

### Definition

Some functions can return different types of objects. A typical C++ solution to this problem is to derive all possible return types from a common base class, to return a pointer to this class and to perform a dynamic cast on this pointer. The class Object provides an abstraction. An object obj of the class Object can represent an arbitrary class. The only operations it provides is to make copies and assignments, so that you can put them in lists or arrays. Note that Object is NOT a common base class for the elementary classes. Therefore, there is no automatic conversion from these classes to Object. Rather this is done with the global function make_object. This encapsulation mechanism requires the use of assign to use the functionality of the encapsulated class.

### Creation

Object obj;
| introduces an uninitialized variable.
Object obj ( o);
| Copy constructor.

Objects of type Object are normally created via the global function make_object.

### Operations

Object & | obj.operator= ( o)
|  | Assignment.

Assignment of an object of type Object to an object of type T is done using assign.

There is also a member function to check whether an object of type Object contains an object.

bool | obj.is_empty () | returns true, if object does not contain an object.

### Example

In the following example, the object class is used as return value for the intersection computation, as there are possibly different return values.

    {
        Point_2< Cartesian<double> > point;
        Segment_2< Cartesian<double> > segment,  segment_1, segment_2;

        std::cin >> segment_1 >> segment_2;

        Object obj = intersection(segment_1, segment_2);

        if (assign(point, obj)) {
            /* do something with point */
        } else if ((assign(segment, obj)) {
            /* do something with segment*/
        }

        /*  there was no intersection */
    }

The intersection routine itself looks roughly as follows:

    template < class Kernel >
    Object  intersection(Segment_2<Kernel> s1, Segment_2<Kernel> s2)
    {

        if (/* intersection in a point */ ) {

           Point_2<Kernel> p = ... ;
           return make_object(p);

        } else if (/* intersection in a segment */ ) {

           Segment_2<Kernel> s = ... ;
           return make_object(s);
        }
        return Object();
    }

### See Also

CGAL::assign
CGAL::make_object
Kernel::Object_2
Kernel::Object_3