operator-overloading

#define a special operator in c++

拟墨画扇 提交于 2019-12-12 09:45:05
问题 Say that I want to make up a special operator !+ in C++ between two objects. I would like to use !+, on example, because I think it is much more meaningful than any other operator. One basic thing I could do is to find a free, unused operator and make the replacement work with a #define: #define !+ % class myclass { public: int operator %(myclass &c) { return 3; } } So that if I later write something like a!+b with a and b instances of myclass, it would work. Now, is there any way to define

How to overload operator<< for qDebug

本秂侑毒 提交于 2019-12-12 09:29:33
问题 I'm trying to create more useful debug messages for my class where store data. My code is looking something like this #include <QAbstractTableModel> #include <QDebug> /** * Model for storing data. */ class DataModel : public QAbstractTableModel { // for debugging purposes friend QDebug operator<< (QDebug d, const DataModel &model); //other stuff }; /** * Overloading operator for debugging purposes */ QDebug operator<< (QDebug d, const DataModel &model) { d << "Hello world!"; return d; } I

how to overload == operator to allow it to be used in multiple comparisons?

拟墨画扇 提交于 2019-12-12 08:16:21
问题 I am trying to overload == operator to compare objects like below. class A { int a; public: A(int x) { a = x; } bool operator==(const A& obRight) { if(a == obRight.a) { return true; } return false; } }; int main() { A ob(10), ob2(10), ob3(10); if(ob == ob2) // This equality comparison compiles fine. cout<<"Equal"<<endl; if(ob == ob2 == ob3) //This line doesn't compile as overloaded // == operator doesn't return object (returns bool) cout<<"Equal"<<endl; } As i described above, i am unable to

Using a (mathematical) vector in a std::map

白昼怎懂夜的黑 提交于 2019-12-12 07:54:18
问题 Related: what can I use as std::map keys? I needed to create a mapping where specific key locations in space map to lists of objects. std::map seemed the way to do it. So I'm keying a std::map on an xyz Vector class Vector { float x,y,z } ; , and I'm making a std::map<Vector, std::vector<Object*> > . So note the key here is not a std::vector , its an object of class Vector which is just a math xyz vector of my own making. To produce a "strictly weak ordering" I've written the following

Comparing objects using bool operator==

夙愿已清 提交于 2019-12-12 07:49:54
问题 So, after reading some SO questions and answers, i still doesn't understand why use friend bool operator==( BaseClass const &left, BaseClass const &right ) instead of bool operator==( BaseClass const &right ) right now I have something like this http://pastebin.com/pKsTabC0 (Fixed) - and it seems to work fine. But maybe I'm missing something? Any Suggestions? Update 1 Ok i changed the source to make it work right http://ideone.com/fIAmB. Removed unnecessary virtual and added const. Still i

Overloading of << operator using iterator as a parameter

廉价感情. 提交于 2019-12-12 06:16:53
问题 I`d like to print enum values as text and use for it overloading. Suppose I have the following code: #include <iostream> #include <map> #include <string> #include <vector> #include <unordered_set> enum enm{ One, Two }; class Complex{ public: friend std::ostream& operator<<(std::ostream& out, std::unordered_multiset<int>::const_iterator i){ switch (*i){ case One:{ return out<<"One"; } case Two:{ return out << "Two"; } } } void func(std::unordered_multiset<int> _v); }; void Complex:: func(std:

Override typecasting with custom operators

拜拜、爱过 提交于 2019-12-12 06:13:16
问题 Let say I have something like this: var x: Int = 6 var y: Float = 11.5 so, the result has to be written like this var result = Float(x) * y or var result = x * Int(y) This makes sense, right ? However, I think that a little clumsy, so I'm trying to make some custom operators for this: infix operator *~ { associativity left precedence 150 } //floating func *~ (lhs: Float, rhs: Int) -> Float { return lhs * Float(rhs) } func *~ (lhs: Int, rhs: Float) -> Float { return rhs * Float(lhs) } infix

how to overload an operator of a generic class with operands of different generic types of the generic class

↘锁芯ラ 提交于 2019-12-12 05:38:10
问题 There have been a lot of similar questions asked but all involve operands of same type or same generic type. This one in particular (How can I use a generic type parameter with an operator overload?) is close to what I am looking for but no answer or work-around. Is it possible to do something like this for the ‘*’ operator overload: public class MyNumericClass<T> { public double Value { get; set; } //OK but works only for same type operands public static double operator *(MyNumericClass<T>

C++ Operator += overload

核能气质少年 提交于 2019-12-12 05:27:50
问题 I want to overload the operator += in the way that when i will use it a+=b; it will add b to the vector a having this in the header: public: ... void Book::operator+=(std::string a, std::vector<std::string>>b); private: ... std::string b; stf::vector<std::string> a; this is the implementation in cpp void Book::operator+=(std::string a, std::vector<std::string>>b) { b.push_back(a); } What can be my error? It is not clear for me the use of overload operators yet 回答1: You can overload the +=

Overloading += operator in C++ - How do you pass the left operand?

谁说我不能喝 提交于 2019-12-12 05:19:02
问题 I need to create an operator that accepts a double 'parameter'. myClass myobject(); double mydouble = 10000; mydouble += myobject; My operator: double operator+=(double value, const myclass& object) { value += object.value; return value; } The parameter value is being passed to the operator += as zero, even though mydouble is initialized to 10000. How do you create an operator that can accept the left operand as a parameter? 回答1: The correct prototype is the following: double& operator+=