Introduction
This is a very crude demonstration of operator overloading. It requires you to have a bit of knowledge on operator overloading before you can fully utilize this class. This class is useful for those who don't remember all the necessary ingredients for the overloading of each operator. It allows you to cut and paste the section of code that you want, and modify it to suit your needs.
The following listing shows both global operator overloading and class member operator overloading. If all the global functions are excluded, the Template class can be compiled. It is a working, although useless, class.
The comments "Reimplement" show those functions which require you to change the implementation before it can be used. Those which have "No change necessary" can be used straight without changing as they depend solely on those functions that need re-implementation unless you feel there is a need to change them to improve performance.
Nothing much more can be said about the listing. Any comments and improvements to the template are much welcome. Let's maintain this reference for all beginners together.
template<class T> T operator+(T x) {return +x;}
template<class T> T operator-(T x) {return -x;}
template<class T> T operator*(T x) {return *x;}
template<class T> T operator&(T x) {return &x;}
template<class T> T operator!(T x) {return !x;}
template<class T> T operator~(T x) {return ~x;}
template<class T> T operator++(T x) {return ++x;}
template<class T> T operator--(T x) {return --x;}
template<class T> T operator++(T x, int) {return x++;}
template<class T> T operator--(T x, int) {return x--;}
template<class T> const T& operator+(const T& x,
const T& y) {T w(x); w += y; return w;}
template<class T> const T& operator-(const T& x,
const T& y) {T w(x); w -= y; return w;}
template<class T> const T& operator*(const T& x,
const T& y) {T w(x); w *= y; return w;}
template<class T> const T& operator/(const T& x,
const T& y) {T w(x); w /= y; return w;}
template<class T> const T& operator%(const T& x,
const T& y) {T w(x); w %= y; return w;}
template<class T> const T& operator^(const T& x,
const T& y) {T w(x); w ^= y; return w;}
template<class T> const T& operator&(const T& x,
const T& y) {T w(x); w &= y; return w;}
template<class T> const T& operator|(const T& x,
const T& y) {T w(x); w |= y; return w;}
template<class T> bool operator==(T x, T y)
const {return x == y;}
template<class T> bool operator< (T x, T y)
const {return x < y;}
template<class T> bool operator!=(T x, T y)
const {return !(x == y);}
template<class T> bool operator> (T x, T y)
const {return (y < x);}
template<class T> bool operator<=(T x, T y)
const {return !(y < x);}
template<class T> bool operator>=(T x, T y)
const {return !(x < y);}
template<class T> const T& operator<<(T x, T y)
{return x << y;}
template<class T> const T& operator>>(T x, T y)
{return x >> y;}
template<class T> const T& operator&&(T x, T y)
{return x && y;}
template<class T> const T& operator||(T x, T y)
{return x || y;}
template<class T> const T& operator,(T x, T y)
{return x , y;}
template<class T> ostream&
operator<<(ostream& os, const T& x)
{os << x; return (os);}
template<class T> istream&
operator>>(istream& is, T& x)
{is >> x; return (is);}
template<class T>
class Template
{
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef T* iterator;
typedef const T* const_iterator;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
iterator begin() {return this;}
const_iterator begin() const {return this;}
iterator end() {return this;}
const_iterator end() const {return this;}
size_type size() const {return size_;}
Template(size_t size=0) : size_(size)
{memberA = new value_type[size];}
Template(const Template& y) {*this=y;}
~Template() {delete[] memberA;}
const Template& operator+() const
{+member; return *this;}
const Template& operator-() const
{~member + 1; return *this;}
const Template& operator*() const
{member; return *this;}
const Template& operator&() const
{&member; return *this;}
const Template& operator~() const
{~member; return *this;}
const Template& operator!() const
{!member; return *this;}
pointer operator->() {return memberA;}
const Template& operator++()
{++member; return *this;}
const Template& operator--()
{--member; return *this;}
Template& operator++(int)
{Template w(*this); ++*this; return w;}
Template& operator--(int)
{Template w(*this); ++*this; return w;}
const Template& operator=(const Template& y)
{
if (this != &y)
{
member = y.member;
size_ = y.size_;
delete[] memberA;
memberA = new value_type[y.size_];
memberA = y.memberA;
}
return *this;
}
Template& operator+= (const Template& y)
{member += y.member; return *this;}
Template& operator-= (const Template& y)
{member -= y.member; return *this;}
Template& operator*= (const Template& y)
{member *= y.member; return *this;}
Template& operator/= (const Template& y)
{member /= y.member; return *this;}
Template& operator%= (const Template& y)
{member %= y.member; return *this;}
Template& operator<<=(size_t y)
{member <<= y; return *this;}
Template& operator>>=(size_t y)
{member >>= y; return *this;}
Template& operator&= (const Template& y)
{member &= y.member; return *this;}
Template& operator|= (const Template& y)
{member |= y.member; return *this;}
Template& operator^= (const Template& y)
{member ^= y.member; return *this;}
Template operator+(const Template& y) const
{Template w(y); w += *this; return w;}
Template operator-(const Template& y) const
{Template w(y); w -= *this; return w;}
Template operator*(const Template& y) const
{Template w(y); w *= *this; return w;}
Template operator/(const Template& y) const
{Template w(y); w /= *this; return w;}
Template operator%(const Template& y) const
{Template w(y); w %= *this; return w;}
Template operator&(const Template& y) const
{Template w(y); w &= *this; return w;}
Template operator|(const Template& y) const
{Template w(y); w |= *this; return w;}
Template operator^(const Template& y) const
{Template w(y); w ^= *this; return w;}
Template operator<<(size_t y) const
{Template w(y); w <<= y; return w;}
Template operator>>(size_t y) const
{Template w(y); w >>= y; return w;}
Template operator&&(const Template& y) const
{Template w(y); member && y.member; return w;}
Template operator||(const Template& y) const
{Template w(y); member || y.member; return w;}
Template operator,(const Template& y) const
{Template w(y); member , y.member; return w;}
bool operator==(const Template& y) const
{return (member == y.member);}
bool operator< (const Template& y) const
{return (member < y.member);}
bool operator!=(const Template& y) const
{return !(*this == y);}
bool operator> (const Template& y) const
{return (y < *this);}
bool operator<=(const Template& y) const
{return !(y < *this);}
bool operator>=(const Template& y) const
{return !(*this < y);}
reference operator[](size_type index)
{return memberA[index];}
const_reference operator[](size_type index)
const {return memberA[index];}
friend ostream& operator<<(ostream& os,
const Template& y)
{os << y.member; return os;}
friend istream& operator>>(istream& is,
Template& y) {is >> y.member; return is;}
const Template& operator()(Template& y)
{*this * y; *this / y; return *this;}
Template& operator->*(Template& y)
{return *this;}
void* operator new(size_t size)
{return ::new Template;}
void* operator new(size_t n, T extra)
{return ::new Template;}
void* operator new[](size_t size)
{return ::new Template[size];}
void* operator new[](size_t index, T extra)
{return ::new Template[size];}
void operator delete(void *p)
{::delete reinterpret_cast<Template*>(p);}
void operator delete(void *p, size_t size)
{::delete reinterpret_cast<Template*>(p);}
void operator delete[](void *p)
{::delete[] reinterpret_cast<Template*>(p);}
void operator delete[](void *p, size_t size)
{::delete[] reinterpret_cast<Template*>(p);}
void operator delete[](void *p, T extra)
{::delete[] reinterpret_cast<Template*>(p);}
private:
T member;
T* memberA;
size_t size_;
};