operator-overloading

Why are the comparison operators not automatically overloaded with IComparable?

时光怂恿深爱的人放手 提交于 2019-12-23 12:27:44
问题 When a class is IComparable , then we know everything to overload the < , > and == operators due to the CompareTo functionality, right? Then why aren't these overloaded automatically? Take a look at the example below: public class MyComparable : IComparable<MyComparable> { public int Value { get; } public MyComparable(int value) { Value = value; } public int CompareTo(MyComparable other) => Value.CompareTo(other.Value); } I was wondering why something like this wouldn't work by default:

Why and how to overload operator<< for printing

喜你入骨 提交于 2019-12-23 10:41:03
问题 I have written a program for implementation of stack. And I have one display function in it. This is how I wrote display function at first: template <class t> void Mystack<t>::display() { for (int i = 0; i <= top; i++) { std::cout << input[i] << " "; } } Then I was suggested by developers to write a display function to be more generic. So I wrote display function as: template <class T> void Mystack<T>::display(std::ostream &os) const { for (int i = 0; i <= top; i++) { os << input[i] << " "; }

boost::lexical_cast not recognizing overloaded istream operator

好久不见. 提交于 2019-12-23 09:39:12
问题 I have the following code: #include <iostream> #include <boost\lexical_cast.hpp> struct vec2_t { float x; float y; }; std::istream& operator>>(std::istream& istream, vec2_t& v) { istream >> v.x >> v.y; return istream; } int main() { auto v = boost::lexical_cast<vec2_t>("1231.2 152.9"); std::cout << v.x << " " << v.y; return 0; } I am receiving the following compile error from Boost: Error 1 error C2338: Target type is neither std::istream able nor std::wistream able This seems straightforward

C# operator overloading with List<T>

五迷三道 提交于 2019-12-23 09:37:20
问题 I'm trying to overload an operator in C# (don't ask why!) that applies to Lists . For example, I'd like to be able to write: List<string> x = // some list of things List<string> y = // some list of things List<string> z = x + y so that 'z' contains all the contents of 'x' followed by the contents of 'y'. I'm aware that there are already ways to combine two lists, I'm just trying to understand how operator overloading works with generic structures. (This is the List class from Systems

Is it possible to overwrite str's % behaviour using __rmod__?

北战南征 提交于 2019-12-23 09:27:52
问题 I'd like to do: x %doSomething% y which is easy enough to do (see code below) for any x and any y except in the case that x is a str. Is there any way (e.g. adding a special method or raising a specific error) to cause old style string formatting to fail (similarly to how 1 %doSomthing fails with a TypeError) and revert to the __rmod__ method defined in the doSomething object? class BinaryMessage(object): def __init__(self, fn): self._fn = fn def __rmod__(self, LHS): return

How can i override a base class's == operator, so the override gets called

删除回忆录丶 提交于 2019-12-23 09:26:38
问题 With code like the following public class Task { string Name; public static bool operator ==(Task t1, Task t2) { return t1.Name = t2.Name && t1.GetType() == t2.GetType(); } } public class TaskA : Task { int aThing; public static bool operator ==(TaskA t1, TaskA t2) { return (Task)t1 == (Task)t2 && t1.GetType() == t2.GetType() && t1.aThing == t2.aThing; } } public class TaskB : Task //more of the same class Stuffin { List<Task> Tasks; void CheckIt() { bool theSame = Tasks[0] == Tasks[1]; } I'm

templated operator() overload C++

六月ゝ 毕业季﹏ 提交于 2019-12-23 08:29:19
问题 someone already asked this question, but the thread ended up with the original question not getting answered. suppose you have this: template<size_t i, class f_type> void call_with_i(f_type f); functor_type is either: a) a struct with a method that has the following signature: template<size_t i> operator()() const; or, b) a function that looks like this: template<size_t i> foo(); I want "call_with_i<42>(foo)" to be equivalent to "foo<42>()", but I can't figure out the right syntax to make

How to overload `float()` for a custom class in Python?

泄露秘密 提交于 2019-12-23 07:39:27
问题 Summary How can I overload the built-in float for my class so when I call float() on an instance of it, my custom function gets called instead of the default built-in? My Class Hi, I was coding my own Fractions class (for arbitrarily-high floating-point operation precision). It goes like this (I haven't yet finished it): class Fractions: """My custom Fractions class giving arbitarilly high precision w/ floating-point arithmetic.""" def __init__(self, num = 0, denom = 1): """Fractions(num = 0,

Operator overloading '+' operator in C++

谁说胖子不能爱 提交于 2019-12-23 06:20:08
问题 I am facing a problem with the code below which is run on Visual Studio 2008. How do I write the function definition for operator + when you have a statement to be overloaded as follows? class Distance { private: int feet,inches; }; main...... Distance Obj, Obj1(2, 2); Obj = 3 + Obj1; // This line here Obj1+3 is easy, but how does this one compiler know that it has to do overloading? Suppose I have to add the value 3 to the data member feet of Obj1 . 回答1: Generally an operator of that form

Is it possible to overload ++ operators in Python?

☆樱花仙子☆ 提交于 2019-12-23 05:40:47
问题 Is it possible to overload ++ operators in Python? 回答1: There is no ++ operator in Python (nor '--'). Incrementing is usually done with the += operator instead. 回答2: Nope, it is not possible to overload the unary ++ operator, because it is not an operator at all in Python. Only (a subset of) the operators that are allowed by the Python syntax (those operators that already have one or more uses in the language) may be overloaded. These are valid Python operators, and this page lists the