operator-overloading

Why/how does Python's print built-in allow the “>>” operator? [duplicate]

[亡魂溺海] 提交于 2019-12-11 07:54:00
问题 This question already has an answer here : How does the right-shift operator work in a python print statement? (1 answer) Closed 6 years ago . I searched around and couldn't find an answer to this either on this site or elsewhere (always a challenge searching for topics involving punctuation chars). I was looking up the StringIO in the Python standard library (here) and one of the examples is this (excerpt): import StringIO output = StringIO.StringIO() output.write('First line.\n') print >

delete overload, recursive overflow

自闭症网瘾萝莉.ら 提交于 2019-12-11 07:48:06
问题 Hey guys i wrote a quick test. I want delete to call deleteMe which will then delete itself. The purpose of this is so i can delete obj normally which are allocated by a lib. (i dont want any crashes due to crt or w/e). With delete this i get a stackoverflow, without it msvc says i leaked 4 bytes. When i dont call test i leak 0. How do i delete w/o a recursion problem? -edit- to make this more clear. I want the LIB to call delete (thus deleteMe) instead of the program due to crt class B {

Operator Overloading: Ostream/Istream

萝らか妹 提交于 2019-12-11 07:23:59
问题 I'm having a bit of trouble with a lab assignment for my C++ class. Basically, I'm trying to get the "cout << w3 << endl;" to work, so that when I run the program the console says "16". I've figured out that I need to use an ostream overload operation but I have no idea where to put it or how to use it, because my professor never spoke about it. Unfortunately, I HAVE to use the format "cout << w3" and not "cout << w3.num". The latter would be so much quicker and easier, I know, but that's not

Overloading overloaded operators

大憨熊 提交于 2019-12-11 07:14:20
问题 #include <iostream> #include <fstream> using namespace std; class binaryOperators { public: int i; binaryOperators (int tempI = 0) { i = tempI; } binaryOperators operator<< (const binaryOperators &right); }; binaryOperators operator<< (const binaryOperators &left, const binaryOperators &right) { cout << "\nOne"; return left; } binaryOperators binaryOperators :: operator<< (const binaryOperators &right) { cout << "\nTwo"; return *this; } int main () { binaryOperators obj; // Compiler's

Implementing __concat__ in Python

蓝咒 提交于 2019-12-11 07:05:26
问题 I tried to implement __concat__ , but it didn't work >>> class lHolder(): ... def __init__(self,l): ... self.l=l ... def __concat__(self, l2): ... return self.l+l2 ... def __iter__(self): ... return self.l.__iter__() ... >>> lHolder([1])+[2] Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unsupported operand type(s) for +: 'lHolder' and 'list' How can I fix this? 回答1: __concat__ is not a special method (http://docs.python.org/glossary.html#term-special-method

ADL for class in anonymous namespace [closed]

北城以北 提交于 2019-12-11 07:05:06
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed last year . Does anybody know why the next piece of code isn't compiled on Clang 4.0.1? I have the next error: call to function 'operator<<' that is neither visible in the template definition nor found by argument-dependent lookup There is some file test.cpp #include <vector> #include <iostream> namespace Wrapper { template

Implement assignment operator for class with large amount of elements

不想你离开。 提交于 2019-12-11 06:57:03
问题 I have a class looking like class A{ double a, b, c, d, e; float af, bf, cf, df, ef; std::vector<double> av, bv, cv, dv, ev; std::vector<std::vector<double>> avv, bvv, cvv, dvv, evv; A(){} A(/*init values*/){/*Initialize all the values above using the input values*/} ~A(){} } Now I would like to implement an assignment operator, such that I can do (in a second class): class B{ private: A ac, bc, dc; public: B(/*init values*/) { ac = A(/*init values*/); bc = A(/*init values*/); dc = A(/*init

why swift compiler behaves differently with equality operator with/without Equatable protocol

痴心易碎 提交于 2019-12-11 06:54:27
问题 I have a very simple class in a Playground in Swift 4.0 that overrides the == operator. I'm not understanding why the Swift complier doesn't behave the same when the class inherits/doesn't inherit Equatable protocol. Here the class when inheriting Equatable protocol class Test: Equatable { var value = 0 init(_ initialValue:Int) { value = initialValue } static func == (lhs:Test, rhs:Test) -> Bool { return lhs.value == rhs.value ? true : false } } let test1 = Test(0) var test4:Test? = nil if

Overload operator [] to replace vector elements

烈酒焚心 提交于 2019-12-11 06:29:43
问题 I have a classA which has a vector< classB* > vObject . class ClassA{ public: ClassB** operator [] (int index); private: vector<ClassB*> vObject }; Let's Assume that vObject is filled with some classB* objects. What I want to do is to be able to replace classB* elements of vector like that: classA_obj[3] = classA_obj[5]; classA_obj[1] = classB_obj; I tried to return a pointer of ClassB Element. Here is my current operator implementation: ClassB** ClassA::operator [](int index){ return

Advice for novices for how to avoid bugs with operator overloading

。_饼干妹妹 提交于 2019-12-11 06:19:39
问题 I got a question from a student of mine asking why the following code results with what he thought to be a mysterious output. Code: #include <iostream> int main() { char op = '+'; int num = 9; std::string res = "a const char* concatenated with a char and std::string " + op + std::to_string(num); std::cout << res << std::endl; } Well, he expected to get: a const char* concatenated with a char and std::string + 9 and couldn't understand why he gets just std::string 9 . It's clear that if it was