Why is the non-const member function being called, instead of the const one?

If you have two overloads that differ only in their const-ness, then the compiler resolves the call based on whether *this is const or not. In your example code, test is not const, so the non-const overload is called. If you did this: testType test; const testType &test2 = test; test2->x(); you should see that … Read more

How can an operator be overloaded for different RHS types and return values?

As of Rust 1.0, you can now implement this: use std::ops::Mul; #[derive(Copy, Clone, PartialEq, Debug)] struct Vector3D { x: f32, y: f32, z: f32, } // Multiplication with scalar impl Mul<f32> for Vector3D { type Output = Vector3D; fn mul(self, f: f32) -> Vector3D { Vector3D { x: self.x * f, y: self.y * f, … Read more

Declare a TDateTime as a Const in Delphi

Ok, my reaction is a bit late, but here’s a solution for the newer Delphi’s. It uses implicit class overloaders so that records of this type can be used as if they are TDateTime variables. TDateRec = record year,month,day,hour,minute,second,millisecond:word; class operator implicit(aDateRec:TDateRec):TDateTime; class operator implicit(aDateTime:TDateTime):TDateRec; // not needed class operator implicit(aDateRec:TDateRec):String; // not needed class … Read more

C++ why the assignment operator should return a const ref in order to avoid (a=b)=c

(x=y) means x.operator=(y), which returns the object x. Therefore, (x=y)=z means (x.operator=(y)).operator=(z). The expression in parens sets x to y and returns x, and then the outer bit sets x to z. It does not set y to z as you might expect, and as the expression x = y = z does. This behavior … Read more

How do I call the original “operator new” if I have overloaded it?

You can’t access them because it isn’t really overloading, it’s replacement. When you define your own ::operator new, the old one goes away. That’s pretty much that. Essentially, you need to call malloc from a custom ::operator new. Not only that, but also follow the directions in 18.4.1.1/4 to properly handle errors: Default behavior: — … Read more

Error calling user-defined operator+ on temporary object when there are extra brackets

It’s a cast syntax. The reason for that is that casting and the unary addition, subtraction and multiplication (the dereference operator) have higher precedence than their binary counterparts. Since the white spaces here don’t matter this can also be read as: A a = (A()) +A(); The cast and unary+ have higher precedence than the … Read more

How to call custom operator with Reflection

C# compiler converts overloaded operator to functions with name op_XXXX where XXXX is the operation. For example, operator + is compiled as op_Addition. Here is the full list of overloadable operators and their respective method names: ┌──────────────────────────┬───────────────────────┬──────────────────────────┐ │ Operator │ Method Name │ Description │ ├──────────────────────────┼───────────────────────┼──────────────────────────┤ │ operator + │ op_UnaryPlus │ Unary │ │ … Read more

What’s the ampersand for when used after class name like ostream& operator

In that case you are returning a reference to an ostream object. Strictly thinking of ampersand as “address of” will not always work for you. Here’s some info from C++ FAQ Lite on references. As far as const goes, const correctness is very important in C++ type safety and something you’ll want to do as … Read more

c++ error C2662 cannot convert ‘this’ pointer from ‘const Type’ to ‘Type &’

CombatEventType getType(); needs to be CombatEventType getType() const; Your compiler is complaining because the function is being given a const object that you’re trying to call a non-const function on. When a function gets a const object, all calls to it have to be const throughout the function (otherwise the compiler can’t be sure that … Read more