operator-overloading

C#: Inherit from Boolean?

只谈情不闲聊 提交于 2019-12-20 00:07:23
问题 (how) can I Inherit from Boolean? (Or make my class comparable to Boolean with '=' Operator) class MyClass : Boolean { public MyClass() { this = true; } } class Program { public Program() { MyClass myClass = new MyClass(); if(myClass == true) //do something... else //do something else... } } 回答1: Simple example: public class MyClass { private bool isTrue = true; public static bool operator ==(MyClass a, bool b) { if (a == null) { return false; } return a.isTrue == b; } public static bool

Overloading += in c++

笑着哭i 提交于 2019-12-19 22:00:32
问题 If I've overloaded operator+ and operator= do I still need to overload operator+= for something like this to work: MyClass mc1, mc2; mc1 += mc2; 回答1: operator+= is not a composite of + and =, therefore you do need to overload it explicitly, since compiler do not know to build puzzles for you. but still you do able to benefit from already defined/overloaded operators, by using them inside operator+=. 回答2: Yes, you need to define that as well. A common trick however, is to define operator+= ,

In-class friend operator doesn't seem to participate in overload resolution

ⅰ亾dé卋堺 提交于 2019-12-19 19:57:23
问题 While writing a CRTP template that enables classes to provide overloads for operator+ based on template arguments, I found that an in-class friend operator doesn't seem to participate in overload resolution if none of it's arguments is of the type of the class it was defined in. Boiled down: enum class FooValueT{ zero, one, two }; class Foo{ FooValueT val_; public: Foo(FooValueT x) : val_(x){}; Foo& operator+=(Foo other){ val_ = (FooValueT)((int)val_ + (int)other.val_); return *this; } /

Is there any reason to not overload operator== as member, when only comparing to other object of the class?

删除回忆录丶 提交于 2019-12-19 19:56:11
问题 I've been combing through the internet to find an answer, but I couldn't find any. The only reasons given seems to be relevant for comparing with objects of different type (e.g. MyClass == int ). But the most common use case is comparing a class instance to another instance of the same class, not to any unrelated type. In other words, I do understand the problems with: struct A { bool operator==(int b); }; But I cannot find any good reason to not use member function in the most obvious use

Ambiguous overload for ‘operator<<’ in ‘std::cout <<

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 18:59:08
问题 I have the following main.cpp file #include "listtemplate.h" //#include <iostream> using namespace std; int main() { int UserChoice; cout << "Hello, World!" << endl; cin >> UserChoice; cout << UserChoice; } In it's current form, everything works. I enter an integer, and that integer is printed to the screen. However, when I uncomment the cout << "Hello, World!" << endl line, I get the following error main.cpp:10: error: ambiguous overload for ‘operator<<’ in ‘std::cout << "Hello, World!"’ I

Ambiguous overload for ‘operator<<’ in ‘std::cout <<

一世执手 提交于 2019-12-19 18:59:06
问题 I have the following main.cpp file #include "listtemplate.h" //#include <iostream> using namespace std; int main() { int UserChoice; cout << "Hello, World!" << endl; cin >> UserChoice; cout << UserChoice; } In it's current form, everything works. I enter an integer, and that integer is printed to the screen. However, when I uncomment the cout << "Hello, World!" << endl line, I get the following error main.cpp:10: error: ambiguous overload for ‘operator<<’ in ‘std::cout << "Hello, World!"’ I

How to implement idiomatic operator overloading for values and references in Rust?

我与影子孤独终老i 提交于 2019-12-19 15:25:24
问题 When implementing a primitive fixed-size vector type ( float2 for example), I want to support the Add and Sub traits. Later, I will want to support Mul and *Assign . Looking up the documentation and other examples, I came up with this: use std::ops::{Add, Sub}; #[derive(Copy, Clone)] struct float2(f64, f64); impl Add for float2 { type Output = float2; fn add(self, _rhs: float2) -> float2 { float2(self.0 + _rhs.0, self.1 + _rhs.1) } } impl Sub for float2 { type Output = float2; fn sub(self,

Variadic template operator<<

余生长醉 提交于 2019-12-19 11:28:13
问题 I'm trying to change some of my functions foo() into operator<<() , simply for the sake of getting some "half C/half C++" code to look more like C++. Happens, though, I've got stuck at the following transformation step : template <class... T> inline const size_t foo(const T&... data) { return sizeof...(T); } struct bar { template <class... T> inline const size_t operator<<(const T&... data) { return sizeof...(T); } }; int main(int argc, char *argv[]) { bar a; std::cout << ">>> length " << foo

understanding the java string with add operator

拜拜、爱过 提交于 2019-12-19 11:23:07
问题 I am trying to understand how the compiler views the following print statements. It is simple yet a bit intriguing. This prints the added value. Convincing enough. System.out.println(1+2); //output: 3 The output from the following looks convincing as well: System.out.println(1+2+"3");//output: 33 My question (based on the behavior above) is here. System.out.println("1"+2+3);//Should be 15 right? NO. It is 123. I tried few other such statements which were along the same lines. I was able to

No viable conversion from std::function to bool

ε祈祈猫儿з 提交于 2019-12-19 10:33:12
问题 The C++11 std::function is supposed to implement operator bool() const, so why does clang tell me there is no viable conversion? #include <functional> #include <cstdio> inline double the_answer() { return 42.0; } int main() { std::function<double()> f; bool yes = (f = the_answer); if (yes) printf("The answer is %.2f\n",f()); } The compiling error is: function_bool.cpp:12:7: error: no viable conversion from 'std::function<double ()>' to 'bool' bool yes = (f = the_answer); ^ ~~~~~~~~~~~~~~~~ 1