.TH "binopsbase" "3bobcat" "2005\-2016" "libbobcat\-dev_4\&.04\&.00\-x\&.tar\&.gz" "Binary Operators" .PP .SH "NAME" binopsbase \- Class template offering class\-type binary operators .PP .SH "SYNOPSIS" \fB#include \fP .br .PP .SH "DESCRIPTION" Classes can overload binary operators\&. A class named \fIClass\fP may overload these binary operators to suit its own needs, allowing, e\&.g\&., two \fIClass\fP type objects to be added after overloading \fIoperator+\fP\&. Operators for the binary operators *, /, %, +, \-, <<, >>, &, |, and ^ (in this man\-page they are generically indicated as the `\fI@\fP\(cq\& operator) can be overloaded by providing various \fIoperator@\fP members and functions\&. .PP If a class supports copy and/or move construction and if it offers a swap member, then binary operators can all be implemented identically, except for the specific details associated with specific binary operators\&. E\&.g\&., if a class contains two \fIint\fP data members addition of objects of that class could simply be defined as adding the corresponding \fIint\fP members, while subtraction could be defined as subtracting the corresponding \fIint\fP members\&. Assuming the existence of a member \fIvoid Class::add(Class const &rhs)\fP that defines the addition of a \fIClass rhs\fP object to the \fI*this\fP object, while merely providing the basic exception guarantee (i\&.e\&., no leakage), then the binary addition operators can be defined like this: .nf Class operator+(Class &&lhs, Class const &rhs) { lhs\&.add(rhs); return std::move(lhs); } Class operator+(Class const &lhs, Class const &rhs) { Class tmp(lhs); tmp\&.add(rhs); return tmp; } .fi Likewise, \fIoperator+=\fP can be defined for lvalue or rvalue \fIClass\fP objects using reference modifiers\&. .PP As binary operators can all be implemented alike, given the specific members implementing the implied operations, these operators can very well be provided using templates\&. .PP By inheriting from the class template \fIBinopsBase\fP classes offering such specific functions can a particular \fIoperator@=\fP then automatically also offers the matching binary operators after including \fIbobcat/binopsbase\fP\&. Since the binary function templates are not instantiated until used their definitions can be processed by the compiler even if a class implements only a subset of the available binary assignment operators\&. .PP To provide a class \fIClass\fP with binary operators (and binary compound operators) the class \fIClass\fP must implement a member \fIvoid swap(Class &other)\fP, and it should implement (a subset of) the following members (preferably in the class\(cq\&s private section): .IP o \fIvoid mul(Class const &rhs)\fP \- to provide operators \fI*\fP and \fI*=\fP .IP o \fIvoid div(Class const &rhs)\fP \- to provide operators \fI/\fP and \fI/=\fP .IP o \fIvoid mod(Class const &rhs)\fP \- to provide operators \fI%\fP and \fI%=\fP .IP o \fIvoid add(Class const &rhs)\fP \- to provide operators \fI+\fP and \fI+=\fP .IP o \fIvoid sub(Class const &rhs)\fP \- to provide operators \fI\-\fP and \fI\-=\fP .IP o \fIvoid shl(Class const &rhs)\fP \- to provide operators \fI<<\fP and \fI<<=\fP .IP o \fIvoid shr(Class const &rhs)\fP \- to provide operators \fI>>\fP and \fI>>=\fP .IP o \fIvoid and_(Class const &rhs)\fP \- to provide operators \fI&\fP and \fI&=\fP .IP o \fIvoid or_(Class const &rhs)\fP \- to provide operators \fI|\fP and \fI|=\fP .IP o \fIvoid xor_(Class const &rhs)\fP \- to provide operators \fI^\fP and \fI^=\fP Note the trailing underscore for the members \fIand_, or_\fP, and \fIxor_\fP: the underscores are used to prevent conflicts with the \fIand, or,\fP and \fIxor\fP keywords\&. .PP Next, \fIClass\fP should publicly inherit from .nf FBB::BinopsBase .fi where \fIint \&.\&.\&.operators\fP lists the specific set of binary operators that are requested\&. E\&.g\&., to provide \fIClass\fP with multiplication, division, modulo, and shit\-operators its class interface should start as shown: .nf class Class: public FBB::BinopsBase\(cq\&> .fi There is no specific ordering required when specifying \fIint \&.\&.\&.operators\fP\&. Note that \fI\(cq\&<\(cq\&\fP and \fI\(cq\&>\(cq\&\fP in the \fIint \&.\&.\&.operators\fP list specify, respectively, the shift\-left and shift\-right operators\&. All other operators are specified by their common operator, specified as character constants\&. .PP If, as advised, the members implementing the required binary operations are declared in the class\(cq\&s private section then the class\(cq\&s interface should declare its base class as a friend\&. E\&.g\&., .nf class Class: public FBB::BinopsBase\(cq\&> { friend FBB::BinopsBase\(cq\&>; public: \&.\&.\&. private: void mul(Class const &rhs); void div(Class const &rhs); void mod(Class const &rhs); void shl(Class const &rhs); void shr(Class const &rhs); }; .fi .PP .SH "INHERITS FROM" .PP Several Internal\-use\-only Bobcat classes, completing the \fIBinopsBase\fP class template\&. .PP .SH "OVERLOADED OPERATORS" .PP Assuming \fIBinopsBase\fP is a base class of the class \fIClass\fP, then for each of the requested binary operators (e\&.g\&., \fI@\fP) the following operators are now available: .PP .nf free operators: Class operator@(Class const &lhs, Class const &rhs); Class operator@(Class &&lhs, Class const &rhs); members: Class &operator@(Class const &rhs) &; Class &&operator@(Class const &rhs) &&; .fi .PP .SH "EXAMPLE" .nf #include \(dq\&\&.\&./binopsbase\(dq\& #include using namespace std; class Demo1: public FBB::BinopsBase { friend FBB::BinopsBase; public: void swap(Demo1 &other) {} private: void add(Demo1 const &rhs) { cout << \(dq\&adding two Demo1 objects\en\(dq\&; } void sub(Demo1 const &rhs) { cout << \(dq\&subtracting two Demo1 objects\en\(dq\&; } }; class Demo2: public FBB::BinopsBase { friend FBB::BinopsBase; public: void swap(Demo2 &other) {} private: void add(Demo2 const &rhs) { cout << \(dq\&adding two Demo2 objects\en\(dq\&; } void xor_(Demo2 const &rhs) { cout << \(dq\&xor\-ing two Demo2 objects\en\(dq\&; } }; int main() { Demo1 d1a, d1b; Demo1 d1c = d1a + d1b; d1a += d1b; d1c = Demo1{} + d1b; Demo2 d2a, d2b; Demo2 d2c = d2a + d2b; d2a ^= d2b; d2c = Demo2{} ^ d2b; } .fi .PP .SH "FILES" \fIbobcat/binopsbase\fP \- defines the binary operator function templates .PP .SH "SEE ALSO" \fBbobcat/binops\fP(3) \fBbobcat\fP(7) .PP .SH "BUGS" None Reported\&. .PP .SH "DISTRIBUTION FILES" .IP o \fIbobcat_4\&.04\&.00\-x\&.dsc\fP: detached signature; .IP o \fIbobcat_4\&.04\&.00\-x\&.tar\&.gz\fP: source archive; .IP o \fIbobcat_4\&.04\&.00\-x_i386\&.changes\fP: change log; .IP o \fIlibbobcat1_4\&.04\&.00\-x_*\&.deb\fP: debian package holding the libraries; .IP o \fIlibbobcat1\-dev_4\&.04\&.00\-x_*\&.deb\fP: debian package holding the libraries, headers and manual pages; .IP o \fIhttp://sourceforge\&.net/projects/bobcat\fP: public archive location; .PP .SH "BOBCAT" Bobcat is an acronym of `Brokken\(cq\&s Own Base Classes And Templates\(cq\&\&. .PP .SH "COPYRIGHT" This is free software, distributed under the terms of the GNU General Public License (GPL)\&. .PP .SH "AUTHOR" Frank B\&. Brokken (\fBf\&.b\&.brokken@rug\&.nl\fP)\&. .PP