operator-overloading

How to do some stuff with values assigned in subscript operator?

孤者浪人 提交于 2019-12-13 06:41:19
问题 Suppose i've data array 0,0,1,1,2,2,5,5,7,7,2,2 as data member in class and i want to define subscript operator in such they [i] returns me 2*i element of array but also i want let user to set elements, so [i] = n, must be applied to both 2*i and 2*i+1. Is it possible to do it with showing to user only subscript operator? 0,0,1,1,2,2,5,5,7,7,2,2 [3] = 4; 0,0,1,1,2,2,4,4,7,7,2,2 another workarounds? and in general it may be not only two elements. 回答1: Indirectly, yes. You can return a

overload greater than operator with or without friend

六眼飞鱼酱① 提交于 2019-12-13 06:07:54
问题 Suppose I have the following class: class Point{ private: int x,y; public: int get_x() const {return x;} int get_y() const {return y;} Point() :x(0),y(0){} Point(int x,int y):x(x),y(y){} Point(const Point& P){ x = P.get_x(); y = P.get_y(); } Point& operator= (const Point& P) { x = P.get_x(); y = P.get_y(); return *this; } friend ostream& operator<<(ostream& os,const Point& P) { os<<"["<<P.get_x()<<", "<<P.get_y()<<"]"; return os; } Point operator - (const Point &P){ return Point(x-P.get_x(),y

c++ set<> of class objects. Using own comparer giving error: C2804: binary 'operator <' has too many parameters

旧城冷巷雨未停 提交于 2019-12-13 05:39:18
问题 I wrote a c++ code as follows: #include<iostream> #include<string> #include<set> using namespace std; class data{ int i; float f; char c; public: data(); data(int i,float f,char c); }; data::data(int i,float f,char c){ this->i=i; this->f=f; this->c=c; }; class LessComparer{ bool operator<( const data& a1, const data& a2 ) const{ return( a1.i < a2.i || (!(a1.i > a2.i) && (a1.f < a2.f)) || (!(a1.i > a2.i) && !(a1.f > a2.f) && (a1.c < a2.c))); } }; int main(){ set<data,LessComparer> s; set<data

C++ Overload Operator = for Pointers does not work/compile properly

末鹿安然 提交于 2019-12-13 05:24:32
问题 I am trying to implement a template Class with an Operator Overload for = so far it works for non pointer elements. For Pointer Elements it doesn't work exactly as I expect it to, so my question is why this is sow and how do I force c++ do it as I want. My template Class: template <class T> class IBag { public: T _val; void Set(T val) { _val = val; } T Get() { return _val; } IBag& operator=(T val) { this->Set(val); return *this; } operator T() { return this->Get(); } }; How it works using the

namespaced class template inheritance in C++

亡梦爱人 提交于 2019-12-13 05:14:49
问题 In an earlier question, I asked asked class template inheritance in C++. I now have an extra level to add! Consider the following code. (Assume the member definitions are present and accurate) namespace Game { namespace Object { template<typename T> class Packable { public: /** * Packs a <class T> into a Packet (Packet << T) * Required for chaining packet packing *************************************************/ virtual sf::Packet& operator <<(sf::Packet& packet) = 0; // Work-horse, must be

Explicit bool operator - cannot return, test, initialize bool

浪尽此生 提交于 2019-12-13 05:13:11
问题 I just tried to use explicit operator bool() for the first time and its behavior is quite unexpected to me. Can someone please shed some light on why the following sections marked with // does not work . The convertible class would e.g. be a smart pointer class with the ability to check for the validity of the contained data. struct convertible { explicit operator bool() const { return ptr; } void* ptr = nullptr; }; bool testReturn() { convertible test; // does not work return test; } bool

Write an oveloaded comparison (==) between two primative types, in C++

纵然是瞬间 提交于 2019-12-13 04:58:21
问题 As pointed out by this article, it is impossible to overload the comparison operator (==) such that both sides could take primitive types. "No, the C++ language requires that your operator overloads take at least one operand of a "class type" or enumeration type. The C++ language will not let you define an operator all of whose operands / parameters are of primitive types." (parashift) I was wondering: **If I really-really needed to compare two primitives in a non-standard way using the ==,

How to implement both scalar and vector addition using the += operator?

狂风中的少年 提交于 2019-12-13 04:28:13
问题 I'm working on a Vector2D class, and I think both vector addition and scalar addition make sense to be implemented with the +=/+ operators. Trouble is, I don't really know how to work around this apparent argument ambiguity, here's what Clang says: vector2d_test.cpp:17:16: error: use of overloaded operator '+=' is ambiguous (with operand types 'Vector2D<float>' and 'int') vector += 1; ~~~~~~ ^ ~~~~~~~ vector2d.hpp:34:18: note: candidate function Vector2D<T>& operator+=(const Vector2D<T>&

Operator overload function

怎甘沉沦 提交于 2019-12-13 03:59:34
问题 I received an error message when compiling the operator-(double value) function code shown below. The code is merely to find The distance of a point from the origin. Please enlighten me on where i've gone wrong and show me how you resolve it. Let me know if you require more info. Thanks! Compilation error msg: Point.cpp: In member function ‘CS170::Point CS170::Point::operator- (double)’: Point.cpp:187:49: error: no matching function for call to ‘CS170::Point::Point(double)’ return Point(sqrt(

C++/CLI: how to overload an operator to accept reference types?

十年热恋 提交于 2019-12-13 03:48:32
问题 I am trying to create a CLI value class c_Location with overloaded operators, but I think I have an issue with boxing. I have implemented the operator overloading as seen in many manuals, so I'm sure this must be right. This is my code: value class c_Location { public: double x, y, z; c_Location (double i_x, double i_y, double i_z) : x(i_x), y(i_y), z(i_z) {} c_Location& operator+= (const c_Location& i_locValue) { x += i_locValue.x; y += i_locValue.y; z += i_locValue.z; return *this; } c