operator-overloading

Check for multiple values when using comparison operators

妖精的绣舞 提交于 2020-01-02 01:16:08
问题 I've always been under the impression that for any comparison statement, i.e. X == Y or X != Y is the format, and you chain statements together with && or || . Is there not some way to write X == (Y || Z) instead of X == Y || X == Z ? Edit : Since it has been established that this is not possible to do cleanly, how else could it be done? 回答1: #include <algorithm> #include <array> #include <string> #include <iostream> #include <initializer_list> template<class Type, class Next> bool is_one_of

Operator Overloading and Linq Sum in C#

末鹿安然 提交于 2020-01-02 01:05:06
问题 I have a custom type ( Money ) that has an implict conversion to decimal and an overloaded operator for + . When I have a list of these types and call the linq Sum method the result is decimal, not Money . How can I give the + operator presidence and return Money from the Sum ? internal class Test { void Example() { var list = new[] { new Money(10, "GBP"), new Money(20, "GBP") }; //this line fails to compile as there is not implicit //conversion from decimal to money Money result = list.Sum(x

Visitor and templated virtual methods

拜拜、爱过 提交于 2020-01-01 08:37:52
问题 In a typical implementation of the Visitor pattern, the class must account for all variations (descendants) of the base class. There are many instances where the same method content in the visitor is applied to the different methods. A templated virtual method would be ideal in this case, but for now, this is not allowed. So, can templated methods be used to resolve virtual methods of the parent class? Given (the foundation): struct Visitor_Base; // Forward declaration. struct Base { virtual

operator[][] C++

孤街浪徒 提交于 2020-01-01 04:59:16
问题 I'd like to overload operator[][] to give internal access to a 2D array of char in C++. Right now I'm only overloading operator[] , which goes something like class Object { char ** charMap ; char* operator[]( int row ) { return charMap[row] ; } } ; It works ok.. Is it possible to override operator[][] though? 回答1: Don’t try to do that – as others have said, overloading operator [] the way you do actually provides the [][] syntax for free. But that’s not a good thing. On the contrary – it

How to write a C++ conversion operator returning reference to array?

依然范特西╮ 提交于 2020-01-01 04:43:05
问题 In C++ one can add implicit-conversion operators in a class or struct. For instance, 3D vector types usually include something like: struct Vector { float x, y, z; operator float * () { return reinterpret_cast<float *>(this); } }; to allow accessing the vector's elements with subscripts, passing to functions that want a pointer, etc. It occurred to me to wonder: can we instead write a conversion operator that returns a reference to array of float, instead of a pointer to float? (This is of

Is it possible to implement events in C++?

拟墨画扇 提交于 2020-01-01 04:14:47
问题 I wanted to implement a C# event in C++ just to see if I could do it. I got stuck, I know the bottom is wrong but what I realize my biggest problem is... How do I overload the () operator to be whatever is in T , in this case int func(float) ? I can't? Can I? Can I implement a good alternative? #include <deque> using namespace std; typedef int(*MyFunc)(float); template<class T> class MyEvent { deque<T> ls; public: MyEvent& operator +=(T t) { ls.push_back(t); return *this; } }; static int test

c++ less operator overload, which way to use?

旧城冷巷雨未停 提交于 2020-01-01 04:04:06
问题 For example: in a C++ header file, if I defined a struct Record and I would like to use it for possible sorting so that I want to overload the less operator . Here are three ways I noticed in various code. I roughly noticed that: if I'm going to put Record into a std::set , map , priority_queue , … containers, the version 2 works (probably version 3 as well); if I'm going to save Record into a vector<Record> v and then call make_heap(v.begin(), v.end()) etc.. then only version 1 works. struct

Class operators

为君一笑 提交于 2019-12-31 07:45:53
问题 I'm having a problem to make the code: void main(){ Matrix c(rows,cols);//rows & cols are int numbers c[0][0]=2//the line that I'm having a problem to do the operator } //My class defined like this: class Matrix{ public: Matrix(int rows,int cols): rows(rows), cols(cols){ mat= new double*[cols]; for( int i=0; i<rows;i++){ *mat=new double[i]; } } private: int rows,cols; double **mat; }; How can I make an operator that will help me to do the line that I'm having a problem with? 回答1: There are no

Addition overloading in Python behaviour x+y vs y+x

青春壹個敷衍的年華 提交于 2019-12-31 06:27:12
问题 In Python if I have class Foo: def __add__(self, other): return 123 then I can do Foo()+1 and get 123. But if I do 1+Foo() I get an exception because int doesn't know how to add Foos. Is there a workaround so that 1+Foo() works too? 回答1: Implement the __radd__() method as well. 来源: https://stackoverflow.com/questions/20832112/addition-overloading-in-python-behaviour-xy-vs-yx

Addition overloading in Python behaviour x+y vs y+x

笑着哭i 提交于 2019-12-31 06:27:06
问题 In Python if I have class Foo: def __add__(self, other): return 123 then I can do Foo()+1 and get 123. But if I do 1+Foo() I get an exception because int doesn't know how to add Foos. Is there a workaround so that 1+Foo() works too? 回答1: Implement the __radd__() method as well. 来源: https://stackoverflow.com/questions/20832112/addition-overloading-in-python-behaviour-xy-vs-yx