operator-overloading

C++ Extraction Operator for Class Functions

蹲街弑〆低调 提交于 2019-12-13 02:47:15
问题 I'm not sure if operator overloading is what I'm looking for, but I need to know the best way to achieve the following in C++; I have a class Employee (for simplicity) with just an ID number atm. Please assume the input file has an int number and some characters after (1 line shown only), such as: 1234 Charles Hammond Here is the code so far. I am trying to use the extraction operator to get the integer and other data from input file to my class function (SetID); class Employee { int

Overloading operator= between objects of more than two classes

倖福魔咒の 提交于 2019-12-13 02:42:53
问题 I'm defining three classes for my application: int2_ (couples of integers), float2_ (couples of floats) and double2_ (couples of doubles), essentially to perform complex arithmetics operations. I want to overload the assignment = operator between objects of the above three classes. The motivation is that I'm writing a CUDA code, but it seems that there is no assignment operator already defined for the CUDA classes int2 , float2 and double2 . My current implementation is the following: class

What is the “best” way to overload arithmetic operators in modern C++?

会有一股神秘感。 提交于 2019-12-13 02:34:26
问题 I want to implement a kind of "number-like" mathematical objects (say, elements in a group or a ring) in C++. I'm sure that I'm not the only one dealing with this problem, so there may be abundant amount of discussions about what is the "best" way of overloading arithmetic operators. However, I couldn't find satisfactory answers (although maybe I was too lazy to googling more). Let's say I want to overload the operator "+" for a class A. The problem is, there are too many different overloads

BigInteger Class Implementation Overload operator '+'

耗尽温柔 提交于 2019-12-13 02:33:09
问题 I want to create a bigInteger class that uses arrays in backend. BigInt a = 4321; // assume in BigInt class: array is {4,3,2,1} BigInt b = 2131; // assume in BignInt class: array is {2,1,3,1} BigInt sum = a + b; // array {6,4,5,1} I wrote this function to overload '+' operator to add the two numbers int* operator+(const uint32& other) const{ uint32 sum[n]; for(int i=0; i<n; i++){ sum[i] = (*this[i]) + other[i]; } return sum; } but it doesn't work. Note : assume the array size to be fixed. My

How (if at all) can binary operator methods of numpy arrays be overridden by the right operand?

送分小仙女□ 提交于 2019-12-13 02:02:53
问题 I have a custom class which defines custom, commutative, behaviour for the binary + operator: class foo(object): def __add__(self, other): ... __radd__ = __add__ It is designed to deal with scalars and with numpy arrays in its own way---a way that is intentionally different from the way numpy adds arrays. If I start with the following: f = foo() a = numpy.array([1, 2, 3]) c = 4 then all is well if I do f + a , f + c or c + f , in the sense that I get the custom behaviour I want in all cases.

c++ delete should do nothing

允我心安 提交于 2019-12-13 01:54:25
问题 Question: is there any way to make delete behave like a dummy i.e do nothing when called? Why I need the answer: I'm using a custom memory pool that frees the all the object from memory when a static method is called Pool::freeAllObjects() . all the classes have an overloaded operator new like the following: void * operator new (size_t size) { void * obj = Pool::memory()->allocate(size); Pool::memory()->save_destructor((ClassName*)obj); return obj; } Pool::memory()->save_destructor() just

Initializer lists and assignment overloading (operator =)

时光总嘲笑我的痴心妄想 提交于 2019-12-12 22:56:46
问题 Does the overloading of the assignment operator propagate to an initializer list? For example, suppose a class: class MyClass { private: std::string m_myString; //std::string overloads operator = public: MyClass(std::string myString); } And a constructor: MyClass::MyClass(std::string myString) : m_myString(myString) { } Will the initializer list work out the assignment operator overload on std::string ? And if not, is there a workaround? Particularly for GCC. 回答1: I think what you are missing

template class operator overloading problem

孤街醉人 提交于 2019-12-12 19:23:53
问题 I'm trying to port some Visual C++ (VS2005) code so that it will compile under both VS2005 and C++ Builder XE. The code that follows compiles fine under VS2005 but under C++ Builder XE, I get the following error: [BCC32 Error] time_stamp.h(49): E2327 Operators may not have default argument values Here is the code in question: (time_stamp.h) template<typename counter_type> class time_stamp { typedef time_span<counter_type> time_span_type; typedef typename counter_type::value_type value_type;

python - overloading several operators at once

巧了我就是萌 提交于 2019-12-12 18:26:40
问题 I have a custom class and I want to overload several artihmetic operators, and wonder if there is a way to avoid having to write out the code for each one individually. I haven't been able to find any examples that don't explicity overload each operator one-by-one. class Foo(object): a=0 def __init__(self, a): self.a=a def __add__(self, other): #common logic here return Foo(self.a+other.a) def __sub__(self, other): #common logic here return Foo(self.a-other.a) def __mul__(self, other):

Friend template overloaded operator <<: unresolved external symbol

这一生的挚爱 提交于 2019-12-12 17:43:55
问题 I'm having issues with the error Error LNK2019 unresolved external symbol "class std::basic_ostream > & __cdecl cop4530::operator<<(class std::basic_ostream > &,class rob::Stack const &)" (??6rob@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV12@ABV?$Stack@H@0@@Z) referenced in function _main Project7 c:\Users\Robrik\documents\visual studio 2015\Projects\Project7\Project7\post.obj 1 Right now, all that post is doing is calling the operator<< The declaration namespace rob { template <