operator-overloading

What are the basic rules and idioms for operator overloading?

牧云@^-^@ 提交于 2019-12-12 05:14:08
问题 Note: The answers were given in a specific order , but since many users sort answers according to votes, rather than the time they were given, here's an index of the answers in the order in which they make most sense: The General Syntax of operator overloading in C++ The Three Basic Rules of Operator Overloading in C++ The Decision between Member and Non-member Common operators to overload Assignment Operator Input and Output Operators Function call operator Comparison operators Arithmetic

How to solve ambiguity in operator overloading embedded inside a struct?

丶灬走出姿态 提交于 2019-12-12 04:22:58
问题 In the following code, the g++ compiler surprisingly cannot decide which operator to use when they are embedded in a struct to serve as a comparator argument in a set: #include <string> #include <set> struct KeyWord { std::string str; int qt; KeyWord(const std::string aKw = "", const int aQt = 0) : str(aKw), qt(aQt) {} }; struct CompareKeywords { bool operator() (const std::string& left, const std::string& right) const { if (left.size() > right.size()) return true; else if (left.size() <

Overloading operator and modifiyng string

偶尔善良 提交于 2019-12-12 04:10:03
问题 I am learning about operator overlaoding. I have created simple class to test it. class Beer{ public: Beer(int oner , int twor , string name){ this -> one = oner; this -> two = twor; this -> name = name; }; int getOne(){ return this -> one; }; int getTwo(){ return this -> two; }; string getName(){ return this -> name; }; Beer operator + (const Beer &a)const { return Beer(5,two+a.two,"firstName"); }; Beer operator + (string a)const { this -> name = this -> name +" "+a; }; private: int one; int

Assignment Operator for an object

女生的网名这么多〃 提交于 2019-12-12 03:54:07
问题 I have written a code, for dynamically allocating a name. I know I should take care of deep copy in such scenarios. What I have written is my own version of Copy Constructor,Copy Assignment Operator and destructor. Should I redefine any other implicit functions such as Move Assignment Operator . I am not clear with the concept of Move Assignment Operator or any other implicitly defined member functions (other than which I have already mentioned ). Can any one please add the code for this

Overloading + operator with classes containing array pointers (C++)

被刻印的时光 ゝ 提交于 2019-12-12 03:53:24
问题 I am currently writing a "Polynomial" class in order to practice Operator Overloading. I have successfully overloaded the stream extraction and insertion operators, but I am having some trouble with the "+" operator. My class has a private pointer that proceeds to create an array in the constructor. I understand how one would overload the "+" operator with a complex number class, for example, but I'm getting confused with this program. Guidance in finding a solution would be greatly

Why isn't my overloading < operator not working for STL sort

时光毁灭记忆、已成空白 提交于 2019-12-12 02:50:41
问题 I have this following code where I want to sort vector of string according to the last character of the string. I've done the following but the sorting is done by default rules. Here's the overloading < part: bool operator<(const string &s1, const string &s2){ return s1.at(s1.size() - 1) < s2.at(s2.size() - 1); } This is in main: vector <string> nameList; int n; cin>>n; while(n--){ string name; char str[100]; cin>>str; name += str; nameList.push_back(name); } sort(nameList.begin(), nameList

What are the basic rules and idioms for operator overloading?

亡梦爱人 提交于 2019-12-12 02:39:14
问题 Note: The answers were given in a specific order , but since many users sort answers according to votes, rather than the time they were given, here's an index of the answers in the order in which they make most sense: The General Syntax of operator overloading in C++ The Three Basic Rules of Operator Overloading in C++ The Decision between Member and Non-member Common operators to overload Assignment Operator Input and Output Operators Function call operator Comparison operators Arithmetic

operator overloading memory leak

妖精的绣舞 提交于 2019-12-12 02:28:31
问题 Recently I got a task to do in C++, implement a Set class with union, intersection etc. as overloaded operators. I've got a problem with overloading an operator+(). I decide to use vectors and get the advantage of some algorithm's library functions. The problem is I HAD TO pass to constructor an array pointer and array size. This complicated this task a bit... I can compile it but during the "z=a+b" operation I encounter somekind of memory leak. Can anyone explain me what am I doing wrong?

Using ofstream* wrapper class with overloaded << operator on endl

谁说我不能喝 提交于 2019-12-12 02:22:31
问题 C++ This is an attempt to make a class that mimics the output behavior of using the << operator of an ofstream , as far as being able to use std::endl and write string s is concerned. The class has a single data member, the ofstream pointer. The class has two overloaded << operators, one that takes an std::string and another that takes a pointer to a function, whose argument is an ostream reference and returns an ostream reference. That is the signature of std::endl , according to this.

Problem with operator <

无人久伴 提交于 2019-12-12 02:08:08
问题 I have a problem with the operator < that i wrote: in Node.h : . .. bool operator<(const Node<T>& other) const; const T& GetData (); . .. template <class T> const T& Node<T>::GetData () { return m_data; } template <class T> bool Node<T>:: operator<(const Node<T>& other) const { return (*(this->GetData()) < *(other.GetData())); } in Heap.h : template<class T> void Heap<T>::Insert(Node<T>* newNode) { if (m_heap.size() == 0) { m_heap.push_back(newNode); } else DecreaseKey(newNode); } template