operator-overloading

Why can some operators only be overloaded as member functions, other as friend functions and the rest of them as both?

戏子无情 提交于 2019-12-17 15:29:29
问题 Why can some operators only be overloaded as member functions, other as non-member "free" functions and the rest of them as both? What is the rationale behind those? How to remember which operators can be overloaded as what (member, free, or both)? 回答1: The question lists three classes of operators. Putting them together on a list helps, I think, with understanding why a few operators are restricted in where they can be overloaded: Operators which have to be overloaded as members. These are

Overloaded inline operators in F#: ( |+| )

霸气de小男生 提交于 2019-12-17 14:53:52
问题 I'm trying to define an overloaded operator, e.g. |+| , as the following: let inline ( |+| ) (m1 : #IMeasurable) (m2 : #IMeasurable) = m1.Measure + m2.Measure The problem is, I can't do something like: let three = m1 |+| m2 |+| m3 Because the operator |+| isn't defined for the case (m1 : int) (m2 : #IMeasurable) . Is there a way to overload this operator or use static type constraints to make the above expression possible? Is there a way to modify IMeasurable (which I can edit) so that this

Golang Operator Overloading

有些话、适合烂在心里 提交于 2019-12-17 14:52:17
问题 I understand that golang does not provide operator overloading, as it believe that it is increasing the complexity. So I want to implement that for structures directly. package main import "fmt" type A struct { value1 int value2 int } func (a A) AddValue(v A) A { a.value1 += v.value1 a.value2 += v.value2 return a } func main() { x, z := A{1, 2}, A{1, 2} y := A{3, 4} x = x.AddValue(y) z.value1 += y.value1 z.value2 += y.value2 fmt.Println(x) fmt.Println(z) } https://play.golang.org/p/1U8omyF8-V

Golang Operator Overloading

别来无恙 提交于 2019-12-17 14:51:37
问题 I understand that golang does not provide operator overloading, as it believe that it is increasing the complexity. So I want to implement that for structures directly. package main import "fmt" type A struct { value1 int value2 int } func (a A) AddValue(v A) A { a.value1 += v.value1 a.value2 += v.value2 return a } func main() { x, z := A{1, 2}, A{1, 2} y := A{3, 4} x = x.AddValue(y) z.value1 += y.value1 z.value2 += y.value2 fmt.Println(x) fmt.Println(z) } https://play.golang.org/p/1U8omyF8-V

How to differentiate (when overloading) between prefix and postfix forms of operator++? (C++)

♀尐吖头ヾ 提交于 2019-12-17 12:35:11
问题 Because I've overloaded the operator++ for an iterator class template<typename T> typename list<T>::iterator& list<T>::iterator::operator++() { //stuff } But when I try to do list<int>::iterator IT; IT++; I get a warning about there being no postifx ++ , using prefix form. How can I specifically overload the prefix/postifx forms? 回答1: Write a version of the same operator overload, but give it a parameter of type int . You don't have to do anything with that parameter's value. If you're

Can I define custom operator overloads in Javascript? [duplicate]

孤街浪徒 提交于 2019-12-17 10:58:45
问题 This question already has answers here : Javascript: operator overloading (5 answers) Overloading Arithmetic Operators in JavaScript? (11 answers) Closed 3 years ago . Is it possible to define custom operators between instances of a type in JavaScript? For example, given that I have a custom vector class, is it possible to use vect1 == vect2 to check for equality, whilst the underlying code would be something like this? operator ==(a, b) { return a.x == b.x && a.y == b.y && a.z == b.z; }

Why is this ambiguity here?

旧时模样 提交于 2019-12-17 10:44:22
问题 Consider I have the following minimal code: #include <boost/type_traits.hpp> template<typename ptr_t> struct TData { typedef typename boost::remove_extent<ptr_t>::type value_type; ptr_t data; value_type & operator [] ( size_t id ) { return data[id]; } operator ptr_t & () { return data; } }; int main( int argc, char ** argv ) { TData<float[100][100]> t; t[1][1] = 5; return 0; } GNU C++ gives me the error: test.cpp: In function 'int main(int, char**)': test.cpp:16: error: ISO C++ says that

C++ template friend operator overloading

瘦欲@ 提交于 2019-12-17 10:42:37
问题 What is wrong with my code? template<int E, int F> class Float { friend Float<E, F> operator+ (const Float<E, F> &lhs, const Float<E, F> &rhs); }; G++ just keeps warning: float.h:7: warning: friend declaration ‘Float<E, F> operator+(const Float<E, F>&, const Float<E, F>&)’ declares a non-template function float.h:7: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables

Using << operator to write to both a file and cout

六眼飞鱼酱① 提交于 2019-12-17 09:58:40
问题 I'd like to overload << operator to write the value it takes to a file and cout. I have tried to do it with following code, but couldn't succeed it. It just writes the value to text file. Any help would be appreciated. Thank you. void operator<<(std::ostream& os, const string& str) { std::cout << str; os << str; } int main() { ofstream fl; fl.open("test.txt"); fl << "!!!Hello World!!!" << endl; return 0; } 回答1: Create a helper class and overload operators that takes care of streaming to two

Overloading operator<< for a templated class

寵の児 提交于 2019-12-17 09:47:12
问题 I'm trying to implement a method for a binary tree which returns a stream. I want to use the stream returned in a method to show the tree in the screen or to save the tree in a file: These two methods are in the class of the binary tree: Declarations: void streamIND(ostream&,const BinaryTree<T>*); friend ostream& operator<<(ostream&,const BinaryTree<T>&); template <class T> ostream& operator<<(ostream& os,const BinaryTree<T>& tree) { streamIND(os,tree.root); return os; } template <class T>