Operators |
CliPP also allows you full operator support. You can overload almost all C++
operators.
class point : public object {
public:
//construction
point(double x,double y,double z);
//operators
point& operator=(const point& point1);
double operator[](int index); //index=0,1,2 returns x_,y_ or z.
vector operator-(const point& point1); //Returns the distance between two points
bool operator!() //Returns true if position lies in origo
//operations
...
//access
...
private:
double x_,y_,z_;
};
Some of the above operators might be rather artificial, but they are useful in illustrating how to expose operators to CliPP:
cls[self = self]; //assignment cls[self[int()]]; //subscript access cls[self - self); //subtraction cls[!self]; //
not
In the example above, self is used to indicate the class you want to
expose. it is required that self is one of the arguments in the
expression.
There are four different types of operators:
Operators with two arguments that do not change the content of any of its arguments.
Example of binary operators include
In C++ these operators can be written as
friend operator op(
const point& lhs,const point& rhs);
friend operator op(
const point& lhs,const vector& rhs);
friend operator op(
const vector& lhs,const point& rhs);
where op is replaced with the correct operator from the list above.
In CliPP the above operators will be exposed to the class point as:
cls[self op self];
cls[self op other<vector>()];
cls[other<vector>() op self];
The other<T> template When exposing an operator, you use self to indicate the class being exposed. If you have an operator involving another type, you need to give this type in as an argument. You can do this by constructing a dummy-variable of the correct type, as in the subscript example above: self[int()] This is very handy if the type has a default constructor such as int, but breaks down for complex types with no default constructors. This is why the other<T> template exists. It is a helper template that propagates the type supplied to other without constructing an object of that type. |
Operators with two arguments that changes the content of its first argument.
Example of binary inplace operators include
In C++ these operators can be written as
point& point::operator op(
const point& rhs);
point& point::operator op(
const vector& rhs);
where op is replaced with the correct operator from the list above.
In CliPP the above operators will be exposed to the class point as:
cls[self op self];
cls[self op other<vector>()];
Operators with one arguments.
Example of binary inplace operators include
In C++ these operators can be written as
point& point::operator op(
);
where op is replaced with the correct operator from the list above.
In CliPP the above operators will be exposed to the class point as:
cls[op self];
In C++ these operators can be written as
point& point::operator ++(
int);
point& point::operator --(
int);
double point::operator [](
int);
In CliPP the above operators will be exposed to the class point as:
cls[self++];
cls[self--];
cls[self[int()]];
Copyright © 2003-2005 Peder Holt
Permission to copy, use, modify, sell and distribute this document
is granted provided this copyright notice appears in all copies. This document
is provided "as is" without express or implied warranty, and with
no claim as to its suitability for any purpose.