operator-overloading

C++: Inheritance and Operator Overloading

只谈情不闲聊 提交于 2019-12-18 13:16:46
问题 I have two structs: template <typename T> struct Odp { T m_t; T operator=(const T rhs) { return m_t = rhs; } }; struct Ftw : public Odp<int> { bool operator==(const Ftw& rhs) { return m_t == rhs.m_t; } }; I would like the following to compile: int main() { Odp<int> odp; odp = 2; Ftw f; f = 2; // C2679: no operator could be found } Is there any way to make this work, or must I define the operator in Ftw as well? 回答1: The problem is that the compiler usually creates an operator= for you (unless

Implementing operator< in C++

混江龙づ霸主 提交于 2019-12-18 12:58:06
问题 I have a class with a few numeric fields such as: class Class1 { int a; int b; int c; public: // constructor and so on... bool operator<(const Class1& other) const; }; I need to use objects of this class as a key in an std::map . I therefore implement operator< . What is the simplest implementation of operator< to use here? EDIT: The meaning of < can be assumed so as to guarantee uniqueness as long as any of the fields are unequal. EDIT 2: A simplistic implementation: bool Class1::operator<

Is there any way in C# to enforce operator overloading in derived classes?

你离开我真会死。 提交于 2019-12-18 12:50:10
问题 I need to define an Interface which has to enforce certain operator overloading to the types which implements it. There doesn't seem an obvious way to do it since operator overloading has to be done using static methods in class. Is there any way to achieve the same effect (using abstract classes or anything else)? 回答1: Bit of a hack, but... You could provide operator overloads in your base class that then call some published abstract methods in one of the classes to do the job there. public

Why overloaded operators cannot be defined as static members of a class?

廉价感情. 提交于 2019-12-18 12:45:16
问题 C++ syntax allows defining overloaded operators either inside the struct/class like: struct X { void operator+(X); } or outside of the struct/class like: void operator+(X, X); but not as struct X { static void operator+(X, X); } Does any body know reasons for this decision? Why the third form is not allowed? (MSVC gives a syntax error). Maybe there is some story behind this? p.s. Presence of the first and the second definitions at the same time creates ambiguity: 1>CppTest1.cxx 1>c:\ballerup

Sorting only using the less-than operator compared to a trivalue compare function

末鹿安然 提交于 2019-12-18 12:28:24
问题 In C++/STL sorting is done by using only the less-than operator. Altough I have no idea how the sorting algorithms are actually implemented, I assume that the other operations are created implicite: a > b *equals* b < a == true a == b *equals* !(a < b) && !(b < a) Compared to using a trivalue* compare function, like for example Java, is this good for performance, or why was this design decision made? My assumption is that any trivalue compareto function still has to implement these

Understanding JavaScript bitwise NOT operator and toString() function

我与影子孤独终老i 提交于 2019-12-18 12:24:56
问题 Thanks to everyone in advance - alert((~1).toString(2)); outputs: -10 But in PHP/Java it outputs 11111111111111111111111111111110 Am I missing something, why does Javascript add a "-" to the output? Thx, Sam 回答1: I know Java uses two's complement to represent negative numbers, and 11111111111111111111111111111110 in binary, which is what ~1 gives, represents -2. Or, represented in binary with a negative sign, -10, which is what you got. The way you calculate the negative of 10 (in base 2)

Guidelines to do constexpr operator-overloading?

馋奶兔 提交于 2019-12-18 10:50:09
问题 Consider a simple int Wrapper class with overloaded multiplication operator*= and operator* . For "old-style" operator-overloading, one can define operator* in terms of operator*= , and there are even libraries like Boost.Operators and its modern incarnation df.operators by @DanielFrey that reduce the boilerplate for you. However, for compile-time computations using the new C++11 constexpr , this convenience disappears. A constexpr operator* cannot call operator*= because the latter modifies

Pointer dereference operator ( (*) vs -> )

浪子不回头ぞ 提交于 2019-12-18 10:48:32
问题 Is there a general difference between doing (*ptr).method() vs ptr->method() I saw this question in a comment on another question and thought I would ask it here. Although I just remembered that pretty much every operator in C++ can be overloaded, so I guess the answer will depend. But in general, is there a difference between doing one versus the other? 回答1: As "jamesdlin" already noted, the * and -> operators can be overloaded for class types. And then the two expressions (*ptr).method()

C++ addition overload ambiguity

隐身守侯 提交于 2019-12-18 09:17:12
问题 I am coming up against a vexing conundrum in my code base. I can't quite tell why my code generates this error, but (for example) std::string does not. class String { public: String(const char*str); friend String operator+ ( const String& lval, const char *rval ); friend String operator+ ( const char *lval, const String& rval ); String operator+ ( const String& rval ); }; The implementation of these is easy enough to imagine on your own. My driver program contains the following: String result

How to Emulate Assignment Operator Overloading in Python?

懵懂的女人 提交于 2019-12-18 09:04:49
问题 How can you emulate assignment operator overloading in Python? For example... class Example(object): name = String() age = Integer() def __init__(self,myname,myage): self.name.value = myname self.age.value = myage Rather than doing self.name.value = name, how can you emulate overloading of the assignment operator so that myname is assigned to self.name.value when you do self.name = myname? 回答1: In this very special case, in attribute assignment, you can use a descriptor. In fact, I suspect