operator-overloading

Const Double Indexing with [][] in Matrix Class

馋奶兔 提交于 2019-12-25 03:13:38
问题 The following code contains a simple example of a Matrix class, with double indexing [][] enabled using a 'proxy' Row class. #include <valarray> #include <iostream> template <typename T> class Matrix { private: // Data members int nRows_; int nColumns_; std::valarray<T> data_; public: // Constructor Matrix(const int nRows, const int nColumns) : nRows_{nRows}, nColumns_{nColumns}, data_{std::valarray<T>(nRows*nColumns)} {} // Row friend class to enable double indexing class Row { friend class

Generic Operator Overloading in Swift

对着背影说爱祢 提交于 2019-12-25 01:57:59
问题 I've been learning Swift and have a question about using Generics with Operator Overloading. This is my requirement: Have a basic generic struct that implements generic matrix functionality, having three main parameters: row:Int, column:Int and array:[T]. Want to implement == operator, i.e. each parameter is ==. Don't want to have to duplicate operator overload functions for each type. It seems Swift isn't smart enough to allow me to write a generic operator overload function that references

Own matrix class multiply operator

笑着哭i 提交于 2019-12-25 01:56:16
问题 I wrote an IntegerMatrix class to add my own methods to work with matrices. Now I've written a function like this: IntegerMatrix** IntegerMatrix::multiplyMatrix(IntegerMatrix** table2) (It's a double pointer because I'm holding a huge array of pointers to 4x4 2D arrays.) so I simply could do this: matrix1.multplyMatrix(matrix2) One little problem is the * isn't defined for my own class. So I thought to overload this operator that I could do something like this: sum += this->table[i][k] *

Overloading operator= between customized classes int2_, float2_ and double2_ for CUDA application

旧巷老猫 提交于 2019-12-25 00:26:56
问题 I'm defining the classes int2_ , float2_ , and double2_ to deal with complex arithmetics in C++ and CUDA. I want to overload the operator = for mixed assignments of objects of the above classes and of the int , float and double types. My implementation is the following: class float2_; class double2_; class int2_ { public: int x; int y; int2_() : x(), y() {} __host__ __device__ inline const int2_& operator=(const int a) { x = a; y = 0.; return *this; } __host__ __device__ inline const int2_&

Typecast operator overloading problem

柔情痞子 提交于 2019-12-24 23:47:48
问题 For example i have two classes A and B, such that for two objects a and b, i want to be able to do : A a; B b; a = b; b = a; for this i have overloaded the = operator, and the typecast operators as: class A{ -snip- operator B()const { return B(pVarA); } }; class B{ -snip- operator A()const { return A(pVarB); } }; but when i try to compile this code, gcc throws the error : error: expected type-specifier before 'B' for the line: operator B()const { return B(pVarA);} my guess is, this is due to

Ambiguous overload when using many typecasts operator overloads

好久不见. 提交于 2019-12-24 18:33:50
问题 I want to create a wrapperClass for strings. I also want the class to be able to return the address of the wrapperClass and the address of the stored (wrapped) string: void FunctionString(string*); void FunctionWrappedString(wrappedString*); int main(){ wrappedString wrappedStringObject; FunctionString(&wrappedStringObject); FunctionWrappedString(&wrappedStringObject); wrappedString anotherWrappedStringObject; if(wrappedStringObject == anotherWrappedStringObject){ // code } } Here are the

C++ reinterpret_cast - will this always work correctly?

有些话、适合烂在心里 提交于 2019-12-24 16:27:36
问题 I have written MyString and MyStringConst class. Now I need from time to time pass MyString as MyStringConst, hence overload cast operator. I have written this MyString::operator const MyStringConst &() const { return reinterpret_cast<const MyStringConst &>(*this); } MyString has this data char * str; int length; volatile int hashCode; int bufferSize; MyStringConst has this data const char * c_str; int length; volatile int hashCode; Plus there are some methods, that in both strings can

Performance of operator overloading vs function call

≡放荡痞女 提交于 2019-12-24 15:51:05
问题 Let us suppose I create my own vector class as follows: <template class T> class Vector { private: void** ptr; // More functions implementing the custom vector public: T& operator[](int iIndex) const { return *(T*)ptr[iIndex]; } T& Item(int iIndex) const { return *(T*)ptr[iIndex]; } } Let say, I have a Vector<someClass> v . Strictly, performance-wise which of these is faster for accessing an element of the vector. v.Item(i) v[i] 回答1: v[i] is merely syntactic sugar for v.operator[](i) . There

Can __radd__ work with the operands in any order?

一笑奈何 提交于 2019-12-24 13:15:53
问题 I want my Fraction class to work as a float when it's being added to floats or integers so I can naturally perform operations with it, but it's only working when the Fraction is the rightmost operand. Is there a way to make it work with the operands in any order or should I override another method that I haven't learned of? Code (I guess variable names are pretty self-explanatory): def __radd__(self,target): if type(target) == int or type(target) == float: return target + self.num/self.den 1

Can I make an assignment operator on a base class that returns sub-class type

狂风中的少年 提交于 2019-12-24 12:36:28
问题 Sorry for the bad title... I have a base class like: template<class T> class GPtr { public: typedef T BaseType; GPtr& operator=(const BaseType& rhs) { ... } }; I often want to make subclassed specializations like: class GraphicPtr : public GPtr<Graphic> { ... }; However my base-class assignment operator still returns GPtr<Graphic> not GraphicPtr and it's annoying to have to copy-paste code in case the core assignment operator functionality should change later. Is there a neat way to define