operator-overloading

Array operator [] overloading const and non-const versions

流过昼夜 提交于 2020-07-11 04:08:42
问题 I got an assignment to implement a template array class. One of the requirement is to overload the [] operator. I made this two const and non-const version which seems to be working fine. const T& operator[](const unsigned int index)const and T& operator[](const unsigned int index) My question is how will the compiler know which one to run when i will do something like: int i=arr[1] On a non-const array? 回答1: The non-const function will always be called on a non-const array, and the const

Array operator [] overloading const and non-const versions

旧城冷巷雨未停 提交于 2020-07-11 04:06:34
问题 I got an assignment to implement a template array class. One of the requirement is to overload the [] operator. I made this two const and non-const version which seems to be working fine. const T& operator[](const unsigned int index)const and T& operator[](const unsigned int index) My question is how will the compiler know which one to run when i will do something like: int i=arr[1] On a non-const array? 回答1: The non-const function will always be called on a non-const array, and the const

Array operator [] overloading const and non-const versions

我是研究僧i 提交于 2020-07-11 04:06:29
问题 I got an assignment to implement a template array class. One of the requirement is to overload the [] operator. I made this two const and non-const version which seems to be working fine. const T& operator[](const unsigned int index)const and T& operator[](const unsigned int index) My question is how will the compiler know which one to run when i will do something like: int i=arr[1] On a non-const array? 回答1: The non-const function will always be called on a non-const array, and the const

Array operator [] overloading const and non-const versions

元气小坏坏 提交于 2020-07-11 04:06:14
问题 I got an assignment to implement a template array class. One of the requirement is to overload the [] operator. I made this two const and non-const version which seems to be working fine. const T& operator[](const unsigned int index)const and T& operator[](const unsigned int index) My question is how will the compiler know which one to run when i will do something like: int i=arr[1] On a non-const array? 回答1: The non-const function will always be called on a non-const array, and the const

C++ std::move a pointer

拈花ヽ惹草 提交于 2020-07-07 18:49:11
问题 I have a C++ framework which I provide to my users, who should use a templated wrapper I wrote with their own implementation as the templated type. The wrapper acts as an RAII class and it holds a pointer to an implementation of the user's class. To make the user's code clean and neat (in my opinion) I provide a cast operator which converts my wrapper to the pointer it holds. This way (along with some other overloads) the user can use my wrapper as if it is a pointer (much like a shared_ptr).

Operator + overloading in base class and using it in derived class

一笑奈何 提交于 2020-07-05 12:55:06
问题 I have 2 classes, base (with copy constructor) and derived, in base Ι have overloaded operator+ : class Base { public: Base(const Base& x) { // some code for copying } Base operator+(const Base &bignum) const { Base x; /* ... */ return x; } }; class Derived : public Base { }; And when i try to do something like that Derived x; Derived y; Derived c=x+y; I get error: conversion from "Base" to non-scalar type "Derived" derived Could problem be in that operator + returns object of Base type and I

Fix use of overloaded operator '+' is ambiguous?

对着背影说爱祢 提交于 2020-06-29 03:57:05
问题 I wrote the following code using C++11 standard: .h file: #include "Auxiliaries.h" class IntMatrix { private: Dimensions dimensions; int *data; public: int size() const; IntMatrix& operator+=(int num); }; Bit I am getting and error saying that: error: use of overloaded operator '+' is ambiguous (with operand types 'const mtm::IntMatrix' and 'int') return matrix+scalar; Any idea of what causes this behaviour and how may I fix it? 回答1: You declared the operators in the mtm namespace so the

Why does cannonical implementation of overloading binary arithmatic operator in C++ pass first arguement by value?

半世苍凉 提交于 2020-06-27 12:44:06
问题 According to this cppreference page, passing the first argument of an overloaded binary operator by value somehow optimizes expressions like a + b + c . The relevant code snippet from the linked page is as follows: class X { public: X& operator+=(const X& rhs) // compound assignment (does not need to be a member, { // but often is, to modify the private members) /* addition of rhs to *this takes place here */ return *this; // return the result by reference } // friends defined inside class

Does it ever make sense to overload unary operator &?

跟風遠走 提交于 2020-06-25 22:14:03
问题 So, C++ allows overloading the unary operator & (address). Are you aware of any real-world example when operator & was rightfully overloaded? And a second, more specific question, are you aware of any real-world example when operator & was rightfully overloaded while preserving address semantics? TIA 回答1: I've got 207 real-world examples of operator &() : Code search 1, Code search 2. Including SafeInt<> (to get the underlying naked integer), boost::gil (apparently also to yield the raw data)

operator Overloading in C#

こ雲淡風輕ζ 提交于 2020-06-22 11:28:25
问题 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