operator-overloading

Why does remove_if( …, lambda ) expression require the assignment operator?

橙三吉。 提交于 2020-01-15 07:17:25
问题 I have this code (simplified) : std::vector<Session> sessions; // ... std::remove_if( sessions.begin(), sessions.end(), [] (const Session& s) { return false; } ); When I compile it (in Visual Studio 2013 Update 1) I get the following error: algorithm(1759): error C2280: 'Session &Session::operator =(const Session &)' : attempting to reference a deleted function Session.h(78) : see declaration of 'Session::operator =' Indeed, I have deleted operator= in the Session class like this: Session&

Python - Add days to an existing date

前提是你 提交于 2020-01-15 06:35:06
问题 Obviously this is homework so I can't import but I also don't expect to be spoon fed the answer. Just need some help on something that's probably pretty simple, but has had me stumped for too many hours now. I have to add days to an existing date in python. Here is my code: class Date: """ A class for establishing a date. """ min_year = 1800 def __init__(self, month = 1, day = 1, year = min_year): """ Checks to see if the date is real. """ self.themonth = month self.theday = day self.theyear

Can't restrict overload of template function `operator+` to my class hierarchy using `enable_if` and `is_base_of`

扶醉桌前 提交于 2020-01-15 06:00:08
问题 I'm creating an operator+ using a template function for a type hierarchy. First simple attempt worked fine until I tried to concatenate a string in another part of the code and GCC 8.3 tried to use my operator+ . Trying to use enable_if and is_base_of to restrict my version to my types so SFINAE handles the issue. One attempt among many is: template < // typename T1, // typename T2, typename std::enable_if_t<std::is_base_of_v<LogicGateAbs, T2>, T2> // > inline OrGate operator +(T1&& lhs, T2&&

Is it okay to use an overloaded operator to implement another operator overload?

夙愿已清 提交于 2020-01-14 17:51:50
问题 For instance if I have overloaded a + operator myClass & operator + (const myClass & rhs) and also overloaded = operator myClass & operator = (const myClass & rhs) both operators are working fine. Can I use this overloaded operator in my += operator overload? myClass & operator += (const myClass & rhs){ *this = *this + progA; return *this; } The above code is working okay. I just want to know if this is good code writing practice or I should re-use the code from the two previous

Is it okay to use an overloaded operator to implement another operator overload?

旧城冷巷雨未停 提交于 2020-01-14 17:51:26
问题 For instance if I have overloaded a + operator myClass & operator + (const myClass & rhs) and also overloaded = operator myClass & operator = (const myClass & rhs) both operators are working fine. Can I use this overloaded operator in my += operator overload? myClass & operator += (const myClass & rhs){ *this = *this + progA; return *this; } The above code is working okay. I just want to know if this is good code writing practice or I should re-use the code from the two previous

C# Overloading Operator == and !=

笑着哭i 提交于 2020-01-14 15:49:08
问题 I am having problems getting the desired behavior out of these few classes and interfaces. Here is my problem, //Inside a Unit Test that has access to internal methods and properties INode firstNode, secondNode; INodeId id = new NodeId (4); first = new Node (id, "node"); second = new Node (id, "node"); Assert.IsTrue (first == second); The assert above is failing because it seems to be going to the object class's equals method instead of the overloaded operator in the Node and NodeId classes.

C# Overloading Operator == and !=

妖精的绣舞 提交于 2020-01-14 15:47:29
问题 I am having problems getting the desired behavior out of these few classes and interfaces. Here is my problem, //Inside a Unit Test that has access to internal methods and properties INode firstNode, secondNode; INodeId id = new NodeId (4); first = new Node (id, "node"); second = new Node (id, "node"); Assert.IsTrue (first == second); The assert above is failing because it seems to be going to the object class's equals method instead of the overloaded operator in the Node and NodeId classes.

Overloading operators for a class

余生长醉 提交于 2020-01-14 07:24:21
问题 Let's say I have the following class: class A { has $.val; method Str { $!val ~ 'µ' } } # Is this the right way of doing it? multi infix:<~>(A:D $lhs, A:D $rhs) { ('(', $lhs.val, ',', $rhs.val, ')', 'µ').join; } How would I overload an operator (e.g., + ) for a class in the same manner as Str in the previous class? I guess this only works for methods that are invoked on an instance object and using the multi operator-type:<OP>(T $lhs, T $rhs) { } syntax for operators is the right way to go

C++ operator== overloading [duplicate]

无人久伴 提交于 2020-01-14 07:11:08
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Operator overloading What is the differences between the following ways to overload operator== ? // stroustrup way friend bool operator== (MyClass &lhs, MyClass &rhs); and // as taught in other places, including caltech bool MyClass::operator== (MyClass &rhs); Which way is better? 回答1: // stroustrup way friend bool operator== (MyClass &lhs, MyClass &rhs); Arguments should be const : friend bool operator==(const

C++ operator== overloading [duplicate]

梦想与她 提交于 2020-01-14 07:11:03
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Operator overloading What is the differences between the following ways to overload operator== ? // stroustrup way friend bool operator== (MyClass &lhs, MyClass &rhs); and // as taught in other places, including caltech bool MyClass::operator== (MyClass &rhs); Which way is better? 回答1: // stroustrup way friend bool operator== (MyClass &lhs, MyClass &rhs); Arguments should be const : friend bool operator==(const