operator-overloading

How to prevent a temporary on the left hand side of the assignment operator

拈花ヽ惹草 提交于 2020-06-17 13:00:42
问题 If I define operator+ for a type, in the usual fashion struct S {}; S operator+(S const &, S const &) { return {}; } users of S can write code like S s{}; s + s = S{}; // huh From what I can tell, operator+ returns a temporary object of type S , which is then assigned to. The object then dies at the end of the statement, because there's no name for it, and so the statement is effectively a no-op. I don't see any use for code like that, so I would like to make that a compile error. Is there a

operator Overloading in C#

微笑、不失礼 提交于 2020-06-17 12:56:23
问题 class Point { private int m_PointX; private int m_PointY; public Point(int x, int y) { m_PointX = x; m_PointY = y; } public static Point operator+(Point point1, Point point2) { Point P = new Point(); P.X = point1.X + point2.X; P.Y = point1.Y + point2.Y; return P; } } Example: Point P1 = new Point(10,20); Point P2 = new Point(30,40) P1+P2; // operator overloading Is it necessary to always declare the operator overloading function as static? What is the reason behind this? If I want to overload

Virtual overloaded operators >> and <<

♀尐吖头ヾ 提交于 2020-06-15 21:22:26
问题 I need an interface that would require its subclasses to overload << and >> , but I'm not quite sure how since these operators aren't overloaded as member functions: std::istream& operator>> (std::istream& in, Student& student) { in >> student.name >> student.group; for (int& i : student.marks) { in >> i; } return in; } Maybe there's a way to make it a member function? 回答1: You could do something like this: class StudentInterface { public: virtual void readSelfFrom(std::istream& in) = 0; };

Canonical implementation of operator+ involves additional move constructor

半腔热情 提交于 2020-06-11 06:09:08
问题 Motivated by this question, I compared two different versions of an implementation of a binary operator+ in terms of operator+= . Consider we are inside the definition of class X . Version 1 friend X operator+(X lhs, const X& rhs) { lhs += rhs; return lhs; } Version 2 friend X operator+(const X& lhs, const X& rhs) { X temp(lhs); temp += rhs; return temp; } friend X operator+(X&& lhs, const X& rhs) { lhs += rhs; return std::move(lhs); } Where, in both cases, operator+= is defined as follows: X

overloading operator<< vs. namespaces

北城以北 提交于 2020-05-13 09:34:19
问题 I have a problem with overloading operator<< combined with namespaces. I have read the related posts, but still do not understand what is going on in my case.. The following code compiles OK: file test_matrix.hpp: #ifndef TEST_MATRIX_HPP #define TEST_MATRIX_HPP #include <boost/numeric/ublas/matrix.hpp> #include <boost/numeric/ublas/matrix_expression.hpp> namespace ublas = boost::numeric::ublas; // shortcut name namespace VecMat { typedef ublas::matrix<double> MatrixD; // matrix of doubles

Operator overloading - why static resolve?

久未见 提交于 2020-05-13 05:05:53
问题 What are the reasons for resolving overloaded operators statically? It seems a strange choice to me - the only advantage I can think of is a small performance gain (but then the JIT may avoid that sometimes as well) at the cost of some rather unintuitive behavior - i.e. I basically have to forward the operator to a virtual function to get the wanted behavior. Was this just taken over from C++ or are there some other good reasons for this? 回答1: See Eric Lipperts article Why are overloaded

“+” in Kotlin Coroutines?

我的梦境 提交于 2020-05-08 04:11:10
问题 This is example code for a Cancellation via explicit job for Kotlin Coroutines: fun main(args: Array<String>) = runBlocking<Unit> { val job = Job() // create a job object to manage our lifecycle // now launch ten coroutines for a demo, each working for a different time val coroutines = List(10) { i -> // they are all children of our job object launch(coroutineContext + job) { // we use the context of main runBlocking thread, but with our own job object delay((i + 1) * 200L) // variable delay

Pointer chain broken when overloading operators

女生的网名这么多〃 提交于 2020-04-17 21:55:58
问题 Got this code (should be all that is relevant): //movable_ptr.hpp //Michal Cermak #ifndef MOVABLE_H #define MOVABLE_H template<typename T> class movable_ptr; template<typename T> class enable_movable_ptr { public: //default constructor enable_movable_ptr() {}; enable_movable_ptr(T* p) : ptr_(p) {}; //operators... T& operator*() const { return *ptr_; }; T* operator->() const { return ptr_; }; bool operator==(const enable_movable_ptr<T>& p) const { return p.ptr_ == ptr_; }; T* get() {return ptr

Pointer chain broken when overloading operators

假如想象 提交于 2020-04-17 21:51:23
问题 Got this code (should be all that is relevant): //movable_ptr.hpp //Michal Cermak #ifndef MOVABLE_H #define MOVABLE_H template<typename T> class movable_ptr; template<typename T> class enable_movable_ptr { public: //default constructor enable_movable_ptr() {}; enable_movable_ptr(T* p) : ptr_(p) {}; //operators... T& operator*() const { return *ptr_; }; T* operator->() const { return ptr_; }; bool operator==(const enable_movable_ptr<T>& p) const { return p.ptr_ == ptr_; }; T* get() {return ptr

C++ Overloading operator << for child classes

孤街醉人 提交于 2020-04-13 17:16:20
问题 I have created a class Location which is a parent class for classes Village and City . I have a vector<Location*> , which contains villages and cities. Now, I need to print to the standard output the content of this vector . This is easy: for (int i = 0; i < locations.size(); i++) cout << locations.at(i); I have overloaded operator << for classes Village , City and Location . It is called overloaded operator << from class Location all the time. I need to call overloaded operator for Village