operator-overloading

Unoverloadable C++ operators

眉间皱痕 提交于 2020-01-03 08:15:09
问题 What operators can not be overloaded in C++? 回答1: From Wikipedia: Operator Name Syntax Bind pointer to member by reference a.*b Member a.b Scope resolution a::b Size of sizeof(a) Ternary a ? b : c Type identification typeid(a) 回答2: I'm pretty sure the C++ FAQ Lite probably covers this. The ones I can think of right off are the ternary operator, the . operator and the scope resolution operator ( :: ). Thinking a moment, since the . operator can't be overloaded, .* probably can't be either.

c++ create, assign and compare a new variable to two object inside an Operator Overloaded function.

女生的网名这么多〃 提交于 2020-01-03 05:16:19
问题 The assignment: Implement an Alien class using a provided Alien.h file. An alien, in this scenario is described in terms of his/her height, weight, and gender. To compare two aliens, you use the following equation to determine the alien’s statusPoints value: statusPoints = weight * height * genderValue where genderValue is 2 if the alien is male, and 3 if the alien is female. The status points should be calculated when needed, not kept as a data member. This avoids so-called stale data, in

What is the minimum set of operators I need to overload?

本小妞迷上赌 提交于 2020-01-03 04:29:05
问题 which operators of the comparison (<=, >, ==, etc.) do You usually implement as your basic operators, which You can after use to implement the rest comparison operators or to make all possible comparisons among the classes? 回答1: You can implement all six of the operators in terms of == and < using the following equivalencies: a != b => !(a == b) a > b => b < a a >= b => !(a < b) a <= b => !(b < a) 回答2: I normally implement operator== for objects and also operator!= . Many objects do not have

Array Subscription: returning Reference vs proxy class method

北城余情 提交于 2020-01-02 19:28:09
问题 While searching for methods for overloading Subscript('[]') operator for template class, I came across two different techniques. First Technique: Overloading the operator [] returning pointer to the container directly, which will allow both reading value and assigning value. A sample implementation of this technique: template <class T> class X { int _size; T *container; public: X(int sz) { _size=sz; container=new T[sz](); } ~X() { } T& operator [](int indx) { return container[indx]; } }; With

In C++, must operator[] () be a member function?

依然范特西╮ 提交于 2020-01-02 07:36:26
问题 In C++, must operator []() always be a member function? If yes, why? I read "An operator must be a member function" in book "The C++ Programming Language Special Edition" page 287. 回答1: From the C++ draft: 13.5.5 Subscripting [over.sub] operator[] shall be a non-static member function with exactly one parameter. It implements the subscripting syntax postfix-expression [ expression ] Thus, a subscripting expression x[y] is interpreted as x.operator for a class object x of type T if T::operator

Free Operators versus Member Operators

青春壹個敷衍的年華 提交于 2020-01-02 05:34:08
问题 class Currency { public: explicit Currency(unsigned int value); // method form of operator+= Currency &operator +=(const Currency &other); // understood! ... }; The following code shows an equivalent API using a free function version of the operator: class Currency { public: explicit Currency(unsigned int value); ... }; // free function form of operator+= Currency &operator +=(Currency &lhs, const Currency &rhs); // ??? Question1 > Why should the free function return Currency& instead of

User-defined implicit conversion of an enum class when calling an overloaded operator fails

∥☆過路亽.° 提交于 2020-01-02 04:03:14
问题 Consider the following example: struct ConvertibleStruct {}; enum class ConvertibleEC {}; struct Target { // Implicit conversion constructors Target(ConvertibleStruct) {} Target(ConvertibleEC) {} }; Target operator~(const Target& t) { return t; } Target anotherFunction(const Target& t) { return t; } int main() { ConvertibleStruct t; ConvertibleEC ec; ~t; // 1. Works finding the operator overloaded above ~ec; // 2. Fails to compile on clang 3.4 and gcc 4.8.2 operator~(ec); // 3. Works finding

Why aren't assignment operators overloadable in VB.NET? [closed]

人盡茶涼 提交于 2020-01-02 03:44:08
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . Why aren't the assignment operators (+=, -=, *=, /=) overloadable in VB.NET? 回答1: Perhaps this is their reasoning: Thanks for the

Using overloaded VB.NET Not operator from C#

和自甴很熟 提交于 2020-01-02 03:12:12
问题 I have a VB class which overloads the Not operator; this doesn't seem to be usable from C# applications. Public Shared Operator Not(item As MyClass) As Boolean Return False End Operator I can use this in VB.NET: If Not MyClassInstance Then ' Do something End If I am trying to us this in a C# application but it won't build. if (!MyClassInstance) { // do something } I get the error Operator '!' cannot be applied to operand of type 'MyClass' Can anyone tell me what I am missing? 回答1: The Not

Using overloaded VB.NET Not operator from C#

孤者浪人 提交于 2020-01-02 03:12:11
问题 I have a VB class which overloads the Not operator; this doesn't seem to be usable from C# applications. Public Shared Operator Not(item As MyClass) As Boolean Return False End Operator I can use this in VB.NET: If Not MyClassInstance Then ' Do something End If I am trying to us this in a C# application but it won't build. if (!MyClassInstance) { // do something } I get the error Operator '!' cannot be applied to operand of type 'MyClass' Can anyone tell me what I am missing? 回答1: The Not