operator-overloading

designing a dot product operator in c++

爷,独闯天下 提交于 2019-12-11 18:13:58
问题 I would like to be able to write statements like a = b.c; where b and c are std::vector and a is their scalar dot product (double). For doing this, I should associate the dot product operation with '.' symbol. Is this possible? 回答1: You cannot override the dot-operator ( . ). Moreover, even if you could you never should for this purpose. When overriding operators, you should follow these three rules : If the meaning of an operator is ambigious, it should not be overloaded. Always conform to

Java - Creating a Pseudo Native Type

女生的网名这么多〃 提交于 2019-12-11 17:26:04
问题 In stock trading, quantities are usually integers (e.g. 5x shares, 10x options, etc). With cryptocurrencies, quantities are fractions (e.g. 0.01 bitcoin). In both scenarios, there is typically a minimum unit (e.g. multiples of 100x shares). I would like to wrap this logic in a Quantity class. However: Java native types (e.g. double ) are final , so I cannot extend them Java does not support operator overloading, so arithmetic will be ugly Java does not support typedefs, so I cannot wrap a

Compound assignment and add operator overloading

我怕爱的太早我们不能终老 提交于 2019-12-11 17:15:11
问题 I need help with both of my operator overloading functions presented below. I'm unsure of how I can implement this without actually using the assignment in the function definitions. Code for operator + in my .cpp file: MyString& MyString::operator +(const MyString& rhs) { delete [] String; String = new char[rhs.Size]; Size = rhs.Size; // needs to be a loop for cascading + // so that String1=String2+String3+String4 will work for(int i = 0; i < rhs.Size+1 ; i++) { // String[i] + rhs.String[i];

Canonical relation operators (==,<,…)

我只是一个虾纸丫 提交于 2019-12-11 13:54:24
问题 Consider a struct (as in: stupid aggergation of several members) with members that all implement a certain relation R (e.g. < ): struct X { A a; B b; }; For most operators there exists a canonical definition for X R X . For instance: bool operator<(X const& x1, X const& x2) { if ((x1.a < x2.a) || (x2.a < x1.a)) // I intentionally did not use != here return x1.a < x2.a; if ((x1.b < x2.b) || (x2.b < x1.b)) return x1.b < x2.b; return false; } This is pretty boring to do for all operators,

C++ Operator Overloading with Primitive Types

久未见 提交于 2019-12-11 12:39:49
问题 How do I define an operator where one operand is a defined type and the other is an int? I have a class Damage and defined as such in Damage.h : #pragma once class Damage { public: Damage(int, int = 0, int = 0); int operator-= (int& num) { return num - this->GetDamage(); } private: int damage; int dotDamage; int dotDamageLength; }; And I am trying to use the operator like this in Monster.cpp : #include "Monster.h" #include "Damage.h" int Monster::TakeDamage(Damage& damage) { this->health -=

C++: operator<< overloading in the nested classes

六月ゝ 毕业季﹏ 提交于 2019-12-11 12:32:24
问题 This question has a detailed answer here: Overloading operator<<: cannot bind lvalue to ‘std::basic_ostream<char>&&’ I am trying to overload a nested subclass, and spent an hour trying to overload operator<< . Researched a little here, but still couldn't resolve it. Any help? :) Whenever I try compiling it using g++ -std=c++11 -lm -ggdb -g -O0 -Wall p_7_1.cpp -o p_7_1 , it gives me an error: Undefined symbols for architecture x86_64: "operator<<(std::__1::basic_ostream<char, std::__1::char

C++ comparison operators

二次信任 提交于 2019-12-11 12:18:04
问题 I frequently see people only overriding operator<, not > or ==. Does it mean that by default, operator> and operator== are implemented using operator< ? I also frequently see people writing (see here) bool operator() (Node const& n1, Node const& n2) const { // TODO: your condition return n1.a < n2.a; } What does operator() mean here then? it seems very counter-intuitive. 回答1: The reason they're only overriding < is because by default that's what ordered containers use to compare values, so

Overloading == operator for class containing only string attributes

♀尐吖头ヾ 提交于 2019-12-11 11:57:32
问题 What would be the best (most elegant or performing) way of overloading the equality operator on a class containing only string attributes? Example: class MagicClass { public string FirstAttribute { get; set; } public string SecondAttribute { get; set; } public string ThirdAttribute { get; set; } public string FourthAttribute { get; set; } public string FifthAttribute { get; set; } } I know how to overload the operator itself, however, I am wondering about the following points: Is there a way

Strange way of overloading the dereference operator from a BoostCon talk

徘徊边缘 提交于 2019-12-11 11:48:47
问题 I was watching a boostcon talk on youtube titled "Introduction to Modern C++ Techniques (Part I)". Around minute 22 the speaker shows a class which overloads the dereference operator. template<typename T, typename CheckingPolicy = NoChecking, typename BadPointerPolicy = BadPointerDoNothing> class pointer_wrapper { public: pointer_wrapper() : value_(0) {} explicit pointer_wrapper(T* p) : value_(p) {} operator T*() { if ( ! CheckingPolicy::check_pointer(value_) ) { return BadPointerPolicy:

C++ 'this' and operator overloading

心已入冬 提交于 2019-12-11 10:55:27
问题 I was wondering how can you use operators along with 'this'. To put an example: class grd : clm { inline int &operator()(int x, int y) { return g[x][y]; } /* blah blah blah */ }; grd grd::crop(int cx1, int cy1, int cx2, int cy2) { grd ngrd(abs(cx1-cx2), abs(cy1-cy2)); int i, j; // for (i = cx1; i < cx2; i++) { for (j = cy1; j < cy2; j++) { if (i >= 0 && i < x && j >= 0 && j < y) ngrd(i-cx1,j-cy2) = ?? // this->(i,j); **** } } return ngrd; } Thanks! 回答1: Either: this->operator()(i,j) or (*this