operator-overloading

Operator '==' can't be applied to type T?

别来无恙 提交于 2019-12-19 05:17:59
问题 I thought this method was valid but I was wrong: static void Equals<T>(T x, T y) { return x == y; //operator == can't be applied to type T } After reading the specifiation (§7.2.4 in v3.0 and §7.3.4 in v4.0): 7.2.4 Binary operator overload resolution An operation of the form x op y, where op is an overloadable binary operator, x is an expression of type X, and y is an expression of type Y, is processed as follows: The set of candidate user-defined operators provided by X and Y for the

STL priority queue and overloading with pointers

元气小坏坏 提交于 2019-12-19 03:13:10
问题 This is my first time using a priority queue. I'm trying to implement Dijkstra's algorithm for school and I figured I need a min heap to do this. Right now my nodes are pointers and I want to compare their weight, but I don't think I can overload > and < with pointers? Is there a way I could accomplish this? Code this far: priority_queue<Node*, vector<Node*>, node_comparison> minHeap; And then I have a struct to compare the node's weights struct node_comparison { bool operator<( const Node* a

Using __str__ representation for printing objects in containers in Python

笑着哭i 提交于 2019-12-19 01:18:11
问题 I've noticed that when an instance with an overloaded __str__ method is passed to the print function as an argument, it prints as intended. However, when passing a container that contains one of those instances to print , it uses the __repr__ method instead. That is to say, print(x) displays the correct string representation of x , and print(x, y) works correctly, but print([x]) or print((x, y)) prints the __repr__ representation instead. First off, why does this happen? Secondly, is there a

size_t parameter new operator

夙愿已清 提交于 2019-12-18 21:36:13
问题 I have a point in my mind which I can't figure out about new operator overloading. Suppose that, I have a class MyClass yet MyClass.h MyClass.cpp and main.cpp files are like; //MyClass.h class MyClass { public: //Some member functions void* operator new (size_t size); void operator delete (void* ptr); //... }; //MyClass.cpp void* MyClass::operator new(size_t size) { return malloc(size); } void MyClass::operator delete(void* ptr) { free(ptr); } //main.cpp //Include files //... int main() {

size_t parameter new operator

♀尐吖头ヾ 提交于 2019-12-18 21:36:08
问题 I have a point in my mind which I can't figure out about new operator overloading. Suppose that, I have a class MyClass yet MyClass.h MyClass.cpp and main.cpp files are like; //MyClass.h class MyClass { public: //Some member functions void* operator new (size_t size); void operator delete (void* ptr); //... }; //MyClass.cpp void* MyClass::operator new(size_t size) { return malloc(size); } void MyClass::operator delete(void* ptr) { free(ptr); } //main.cpp //Include files //... int main() {

C++ - Overloading [] operators based on the side of assignment

不问归期 提交于 2019-12-18 20:02:11
问题 I'm trying to write a dynamic array template in c++ I'm currently overloading the [] operators and I'd like to implement a different behavior based on which side of assignment they are used on. #include <iostream> ... template <class T> T dynamic_array<T>::operator[](int idx) { return this->array[idx]; } template <class T> T& dynamic_array<T>::operator[](int idx) { return this->array[idx]; } using namespace std; int main() { dynamic_array<int>* temp = new dynamic_array<int>(); // Uses the T&

C++ operator overloading: no known conversion from object to reference?

有些话、适合烂在心里 提交于 2019-12-18 19:02:47
问题 When I try to compile the following (g++ 4.6.3) class A {}; A& operator*=( A& a, const A& b ) { return a; } A operator*( const A& a, const A& b ) { return A( a ) *= b; } int main( int, char*[] ) { A a, b; a = a*b; return 0; } I get the error /tmp/test.cxx: In function ‘A operator*(const A&, const A&)’: /tmp/test.cxx:14:20: error: no match for ‘operator*=’ in ‘(* & a) *= b’ /tmp/test.cxx:14:20: note: candidate is: /tmp/test.cxx:6:1: note: A& operator*=(A&, const A&) /tmp/test.cxx:6:1: note: no

Why can I use initializer lists on the right-hand side of operator += but not operator+?

荒凉一梦 提交于 2019-12-18 18:39:54
问题 This is a follow-up to an earlier question about why I can't use a brace-enclosed initializer as an argument to operator+, which was resolved by looking at this earlier question on the subject. Consider the following C++ code, which you can try live at ideone.com: #include <iostream> #include <initializer_list> using namespace std; struct AddInitializerList { void operator+= (initializer_list<int> values) { // Do nothing } void operator+ (initializer_list<int> values) { // Do nothing } }; int

overloaded operator << on ofstream concatenation problems

孤者浪人 提交于 2019-12-18 16:50:59
问题 I have the following code: struct simple { simple (int a1, int a2) : member1(a1), member2(a2) {} int member1; int member2; }; std::ofstream &operator << (std::ofstream &f, const simple &obj) { f<<obj.member1<<", "<<obj.member2; return f; } int main(int argc, const char *argv[]) { std::ofstream f("streamout.txt"); simple s(7,5); f << s; //#1 This works f << "label: " << s; //#2 This fails return 0; } I'm trying to understand why #1 works, while there are problems when trying to use the

Error while overloading operator (must be a nonstatic member function)

瘦欲@ 提交于 2019-12-18 14:52:35
问题 I'm writing string class on my own. And I have such code. I just want to overload operator= . This is my actual code, and I get error in last part of code. #include <iostream> #include <string.h> #include <stdlib.h> using namespace std; class S { public: S(); ~S() { delete []string;} S &operator =(const S &s); private: char *string; int l; }; S::S() { l = 0; string = new char[1]; string[0]='\0'; } S &operator=(const S &s) { if (this != &s) { delete []string; string = new char[s.l+1]; memcpy