Is there a workaround for overloading the assignment operator in C#?

you can use the ‘implicit’ keyword to create an overload for the assignment: Suppose you have a type like Foo, that you feel is implicitly convertable from a string. You would write the following static method in your Foo class: public static implicit operator Foo(string normalString) { //write your code here to go from string … Read more

operator new overloading and alignment

This is a possible solution. It will always choose the operator with the highest alignment in a given hierarchy: #include <exception> #include <iostream> #include <cstdlib> // provides operators for any alignment >= 4 bytes template<int Alignment> struct DeAllocator; template<int Alignment> struct DeAllocator : virtual DeAllocator<Alignment/2> { void *operator new(size_t s) throw (std::bad_alloc) { std::cerr << … Read more

Why/When in Python does `x==y` call `y.__eq__(x)`?

You’re missing a key exception to the usual behaviour: when the right-hand operand is an instance of a subclass of the class of the left-hand operand, the special method for the right-hand operand is called first. See the documentation at: http://docs.python.org/reference/datamodel.html#coercion-rules and in particular, the following two paragraphs: For objects x and y, first x.__op__(y) … Read more

Pointer dereference operator ( (*) vs -> )

As “jamesdlin” already noted, the * and -> operators can be overloaded for class types. And then the two expressions (*ptr).method() and ptr->method() can have different effect. However, for the built-in operators the two expressions are equivalent. The -> operator is more convenient when you’re following a chain of pointers, because . has higher precedence … Read more

Why is this ambiguity here?

It’s actually quite straight forward. For t[1], overload resolution has these candidates: Candidate 1 (builtin: 13.6/13) (T being some arbitrary object type): Parameter list: (T*, ptrdiff_t) Candidate 2 (your operator) Parameter list: (TData<float[100][100]>&, something unsigned) The argument list is given by 13.3.1.2/6: The set of candidate functions for overload resolution is the union of the … Read more

How to overload |= operator on scoped enum?

inline NumericType& operator |=(NumericType& a, NumericType b) { return a= a |b; } This works? Compile and run: (Ideone) #include <iostream> using namespace std; enum class NumericType { None = 0, PadWithZero = 0x01, NegativeSign = 0x02, PositiveSign = 0x04, SpacePrefix = 0x08 }; inline NumericType operator |(NumericType a, NumericType b) { return static_cast<NumericType>(static_cast<int>(a) | … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)