operator-overloading

Implicit array casting in C#

坚强是说给别人听的谎言 提交于 2019-12-22 09:13:14
问题 I have the following classes with an implicit cast operator defined: class A { ... } class B { private A m_a; public B(A a) { this.m_a = a; } public static implicit operator B(A a) { return new B(a); } } Now, I can implicitly cast A to B. But why can't I implicitly cast A[] to B[] ? static void Main(string[] args) { // compiles A a = new A(); B b = a; // doesn't compile A[] arrA = new A[] {new A(), new A()}; B[] arrB = arrA; } Thanks, Malki. 回答1: As Mehrdad Afshari mentioned, you're out of

Is there any way to call class operators with out using * , in a pointer to class type?

痴心易碎 提交于 2019-12-22 08:09:24
问题 Is it possible to call operator[] with out using * when I have a pointer to the class ? class MyClass { public: void operator[](int n) { cout<<"In []"; } }; int main() { MyClass *a=new MyClass; (*a)[2];//work a[2];//It just do some pointer arithmetic ...too bad :(( } 回答1: Yes, you should be able to use the -> operator, like this: a->operator[] (2); Demo on ideone. If all you need is eliminating the asterisk, this should do the trick. If you are aiming for a better readability, this isn't of

Fast vector struct that allows [i] and .xyz-operations in D?

久未见 提交于 2019-12-22 06:46:37
问题 I'd like to create a vector struct in D that works like this: vec u, v; vec w = [2,6,8]; v.x = 9; // Sets x v[1] = w.y; // Sets y u = v; // Should copy data Later I'd also like to add stuff like u = v * u etc. But the above will do for now. This is how far I've come: struct vec3f { float[3] data; alias data this; @property { float x(float f) { return data[0] = f; } float y(float f) { return data[1] = f; } float z(float f) { return data[2] = f; } float x() { return data[0]; } float y() {

C# + operator calls string.concat function? [duplicate]

旧城冷巷雨未停 提交于 2019-12-22 06:25:14
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Does C# optimize the concatenation of string literals? I just found out that we write a line like this: string s = "string"; s = s + s; // this translates to s = string.concat("string", "string"); However I opened the string class through reflector and I don't see where this + operator is overloaded? I can see that == and != are overloaded. [TargetedPatchingOptOut("Performance critical to inline across NGen

C++ Symmetric Binary Operators with Different Types

此生再无相见时 提交于 2019-12-22 05:54:11
问题 I am learning C++ and I was wondering if I could gain some insight into the preferred way of creating binary operators that work on instances of two different types. Here is an example that I've made to illustrate my concerns: class A; class B; class A { private: int x; public: A(int x); int getX() const; int operator + (const B& b); }; class B { private: int x; public: B(int x); int getX() const; int operator + (const A& A); }; A::A(int x) : x(x) {} int A::getX() const { return x; } //

Inability to overload Dot '.' operator in c++

我们两清 提交于 2019-12-22 05:51:09
问题 I have hard time understanding the explanation from stroustrup for what difficulties one must have faced, if operator overloading for '.' was allowed. See this quote from Bjarne Stroustrup: Operator . (dot) could in principle be overloaded using the same technique as used for ->. However, doing so can lead to questions about whether an operation is meant for the object overloading . or an object referred to by . For example: class Y { public: void f(); // ... }; class X { // assume that you

Operator overloading by value results in use of moved value

流过昼夜 提交于 2019-12-22 04:42:53
问题 Compiling the following Rust code that uses operator overloading use std::ops::{Add}; #[derive(Show)] struct Point { x: int, y: int } impl Add for Point { type Output = Point; fn add(self, other: Point) -> Point { Point {x: self.x + other.x, y: self.y + other.y} } } fn main() { let p: Point = Point {x: 1, y: 0}; let pp = p + p; } Results in compiler errors due to ownership of p: <anon>:21:18: 21:19 error: use of moved value: `p` <anon>:21 let pp = p + p; ^ <anon>:21:14: 21:15 note: `p` moved

Overloading operator<< for a nested private class possible?

烈酒焚心 提交于 2019-12-22 03:53:09
问题 How one can overload an operator<< for a nested private class like this one? class outer { private: class nested { friend ostream& operator<<(ostream& os, const nested& a); }; // ... }; When trying outside of outer class compiler complains about privacy: error: ‘class outer::nested’ is private 回答1: You could make the operator<< a friend of outer as well. Or you could implement it completely inline in nested , e.g.: class Outer { class Inner { friend std::ostream& operator<<( std::ostream&

Does Typescript have Operator Overloading?

做~自己de王妃 提交于 2019-12-22 02:00:52
问题 My question is if there is operator overloading in typescript, if it exists I could give an example or a link where you can read about it. 回答1: No it does not exist. It is very unlikely that it will exist unless there is a clear Spec on how it might be implemented in Pure JavaScript. 来源: https://stackoverflow.com/questions/36110070/does-typescript-have-operator-overloading

Int Argument in operator++

微笑、不失礼 提交于 2019-12-22 01:43:18
问题 class myClass { public: void operator++() { // ++myInstance. } void operator++(int) { // myInstance++. } } Besides letting the compiler distinguish between myInstance++ and ++myInstance , is the optional int argument in operator++ actually for anything? If so, what is it? 回答1: As @Konrad said, the int argument is not used for anything, other than to distingush between the pre-increment and post-increment forms. Note however that your operators should return a value. Pre-increment should