.TH "binopsbase" "3bobcat" "2005\-2020" "libbobcat\-dev_5\&.07\&.00" "Binary Operators" .PP .SH "NAME" BinopsBase \- Class template offering class\-type binary operators .PP .SH "SYNOPSIS" \fB#include \fP .br .PP .SH "DESCRIPTION" Classes may overload binary operators\&. A class named \fIDerived\fP may overload binary operators to suit its own needs\&. It could, e\&.g\&., allow two \fIDerived\fP class objects to be added together, or it could define a shift\-operation given a \fIsize_t\fP right\-hand side (rhs) argument\&. .PP The available binary operators are *, /, %, +, \-, <<, >>, &, |, and ^ (in this man\-page they are generically indicated as the `\fI@\fP\(cq\& operator)\&. In addition, overloaded \fIoperator<<\fP and \fIoperator>>\fP for stream insertion and extraction are frequently defined\&. .PP If a class \fIDerived\fP supports copy and/or move construction and if it offers a swap member (\fIvoid Derived::swap(Derived &rhs)\fP), and is publicly derived from \fIFBB::BinopsBase\fP then once \fIDerived\fP defines a member .nf void Class::operator@=(Rhs const &rhs) && .fi defining the compound \fI@\fP\-operator for anonymous, temporary \fIDerived\fP objects and a \fIRhs\fP type for its right\-hand side operand the following operators are also available: .nf Derived &operator@=(Rhs const &rhs) & Derived operator@(Derived &&lhs, Rhs const &rhs); Derived operator@(Derived const &lhs, Rhs const &rhs); .fi .PP A similar procedure applies to the insertion and extraction operators\&. Insertion and extraction operators become available once \fIBinopsBase\fP is declared a friend class of \fIDerived\fP\&. To make the insertion operator available a private member .nf void Derived::insert(std::ostream &out) const .fi must then also be defined, inserting the calling object into \fIout\fP\&. Analogously, to make the extraction operator available a private member .nf void Derived::extract(std::istream &in) .fi must be defined extrating the calling object from \fIin\fP\&. .PP .SH "INHERITS FROM" .PP \-\- .PP .SH "OVERLOADED OPERATORS" .PP For each defined \fIDerived &&operator@=(Rhs const &rhs) &&\fP, defined in the class \fIDerived\fP the following operators are automatically also available: .nf Derived operator@(Derived &&lhs, Rhs const &rhs); Derived operator@(Derived const &lhs, Rhs const &rhs); .fi and .nf Derived &operator@=(Rhs const &rhs) &; .fi .PP The insertion operator becomew available once .nf void Derived::insert(std::ostream &out) const .fi (inserting the calling object into \fIout\fP) has been defined\&. Analogously, the extraction operator becomes available after defining a private member .nf void Derived::extract(std::istream &in) .fi (extracting the calling object from \fIin\fP)\&. .PP .SH "FRIEND DECLARATION" .PP To make the insertion and/or extraction operators available the class \fIDerived\fP must also declare .nf friend FBB::BinopsBase; .fi .PP .SH "EXAMPLE" .nf #include #include class Demo1: public FBB::BinopsBase { friend FBB::BinopsBase; // for insert/extract public: void swap(Demo1 &other) {} Demo1 &&operator+=(Demo1 const &rhs) && { std::cout << \(dq\&adding two Demo1 objects\en\(dq\&; return std::move(*this); } // Explicit definitions take precedence over functions templates // instantiated from definitions in bobcat/binopsbase\&. // Demo1 &operator+=(Demo1 const &rhs) & // { // std::cout << \(dq\&adding (self\-defined) two Demo1 objects\en\(dq\&; // return *this; // } Demo1 &&operator\-=(Demo1 const &rhs) && { std::cout << \(dq\&subtracting two Demo1 objects\en\(dq\&; return std::move(*this); } Demo1 &&operator<<=(Demo1 const &rhs) && { std::cout << \(dq\&shiftleft on two Demo1 objects\en\(dq\&; return std::move(*this); } Demo1 &&operator<<=(size_t rhs) && { std::cout << \(dq\&shiftleft Demo1 object size_t bits\en\(dq\&; return std::move(*this); } void insert(std::ostream &out) const // requires friend { out << \(dq\&inerting a Demo1 object\en\(dq\&; } void extract(std::istream &in) // requires friend { std::cout << \(dq\&extracting a Demo1 object\en\(dq\&; } }; class Demo2: public FBB::BinopsBase { public: void swap(Demo2 &other) {} Demo2 &&operator+=(Demo2 const &rhs) && { std::cout << \(dq\&adding two Demo2 objects\en\(dq\&; return std::move(*this); } Demo2 &operator+=(Demo2 const &rhs) & { std::cout << \(dq\&adding (self\-defined) two Demo2 objects\(dq\&; return *this; } Demo2 &&operator^=(Demo2 const &rhs) && { std::cout << \(dq\&xor\-ing two Demo2 objects\en\(dq\&; return std::move(*this); } Demo2 &&operator|=(Demo2 const &rhs) && { std::cout << \(dq\&or\-ing two Demo2 objects\en\(dq\&; return std::move(*this); } }; int main() { Demo1 d1a, d1b; Demo1 d1c = d1a + d1b; d1a += d1b; d1c = Demo1{} + d1b; std::cout << \(dq\&Here we are \(dq\& << d1c << \(cq\&\en\(cq\&; std::cin >> d1c; d1a <<= d1a; d1a <<= 5; Demo2 d2a, d2b; Demo2 d2c = d2a + d2b; d2a ^= d2b; d2c = Demo2{} ^ d2b; d2c = d2c | 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 "BOBCAT PROJECT FILES" .PP .IP o \fIhttps://fbb\-git\&.gitlab\&.io/bobcat/\fP: gitlab project page; .IP o \fIbobcat_5\&.07\&.00\-x\&.dsc\fP: detached signature; .IP o \fIbobcat_5\&.07\&.00\-x\&.tar\&.gz\fP: source archive; .IP o \fIbobcat_5\&.07\&.00\-x_i386\&.changes\fP: change log; .IP o \fIlibbobcat1_5\&.07\&.00\-x_*\&.deb\fP: debian package containing the libraries; .IP o \fIlibbobcat1\-dev_5\&.07\&.00\-x_*\&.deb\fP: debian package containing the libraries, headers and manual pages; .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