operator-overloading

Why should I overload a C++ operator as a global function (STL does) and what are the caveats?

回眸只為那壹抹淺笑 提交于 2019-12-18 08:54:26
问题 Why would I want to overload a C++ operator() as global and not member function. For example, the == operator. Why is this done? for example in STL libraries. 回答1: The usual rule is for operators which modify the left hand object to be members, and binary operators which return a new object to be free functions; the main motivation for the latter is because the compiler will not convert the left hand side to match a member; if your class supports any implicit conversions, then all of the

Why should I overload a C++ operator as a global function (STL does) and what are the caveats?

拜拜、爱过 提交于 2019-12-18 08:54:20
问题 Why would I want to overload a C++ operator() as global and not member function. For example, the == operator. Why is this done? for example in STL libraries. 回答1: The usual rule is for operators which modify the left hand object to be members, and binary operators which return a new object to be free functions; the main motivation for the latter is because the compiler will not convert the left hand side to match a member; if your class supports any implicit conversions, then all of the

Strange error with a templated operator overload

余生颓废 提交于 2019-12-18 07:39:58
问题 When I compile the following snippet, I get a compiler error with clang, but not with g++/MSVC: #include <string> template<typename T> struct Const { explicit Const(T val) : value(val) {} T value; }; template<typename T> struct Var { explicit Var(const std::string &n) : name(n) {} std::string name; }; template<typename L, typename R> struct Greater { Greater(L lhs, R rhs) : left(lhs), right(rhs) {} L left; R right; }; template<typename L> Greater<L, Const<int> > operator > (L lhs, int rhs) {

php overload = operator [duplicate]

巧了我就是萌 提交于 2019-12-18 07:39:32
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Operator Overloading in PHP Is there a way to overload the = operator ? So want I is the following: class b{ function overloadis(){ // do somethng } } $a = new b(); $a = 'c'; In the example above, I want that when $a = 'c'; is called, the method overloadis is called first and then that function desides if the action (assign 'c' to $a) is executed or aborted. Is it possible to do this ? Thnx in advance, Bob 回答1:

Why isn't operator[] overloaded for lvalues and rvalues?

☆樱花仙子☆ 提交于 2019-12-18 07:30:11
问题 The standard C++ containers offer only one version of operator[] for containers like vector<T> and deque<T> . It returns a T& (other than for vector<bool> , which I'm going to ignore), which is an lvalue. That means that in code like this, vector<BigObject> makeVector(); // factory function auto copyOfObject = makeVector()[0]; // copy BigObject copyOfObject will be copy constructed. Given that makeVector() returns an rvalue vector , it seems reasonable to expect copyOfObject to be move

Why isn't operator[] overloaded for lvalues and rvalues?

烈酒焚心 提交于 2019-12-18 07:30:07
问题 The standard C++ containers offer only one version of operator[] for containers like vector<T> and deque<T> . It returns a T& (other than for vector<bool> , which I'm going to ignore), which is an lvalue. That means that in code like this, vector<BigObject> makeVector(); // factory function auto copyOfObject = makeVector()[0]; // copy BigObject copyOfObject will be copy constructed. Given that makeVector() returns an rvalue vector , it seems reasonable to expect copyOfObject to be move

Operator << overloading

瘦欲@ 提交于 2019-12-18 07:24:32
问题 I'm working on my project and I want to overload operator << which should print out my object on console. Object is called "config" and in its template class it has 4 arrays called attribute1 , attribute2 . attribute3 and attribute4 . So far I've tried this : #include "stdafx.h" #include <string> #include <iostream> using namespace std; template<typename T> class config { T *attribute1, *attribute2, *attribute3, *attribute4; string attribName1, attribName2, attribName3, attribName4; public:

Ruby method for +=

穿精又带淫゛_ 提交于 2019-12-18 05:02:27
问题 Is there a way to make Ruby able to do something like this? class Plane @moved = 0 @x = 0 def x+=(v) # this is error @x += v @moved += 1 end def to_s "moved #{@moved} times, current x is #{@x}" end end plane = Plane.new plane.x += 5 plane.x += 10 puts plane.to_s # moved 2 times, current x is 15 回答1: The += operator is not associated to any method, it is just syntactic sugar, when you write a += b the Ruby interpreter transform it to a = a + b , the same is for a.b += c that is transformed to

Why standard container iterators don't overload `->*`?

吃可爱长大的小学妹 提交于 2019-12-18 04:41:34
问题 Apparently ->* doesn't work automagically if you overload -> , and has to be overloaded manually. Why iterators for standard containers don't overload ->* in addition to -> , forcing usage of (*iter).*mem_ptr instead of iter->*mem_ptr ? #include <iostream> #include <vector> struct S { int x; }; int main() { std::vector<S> vec = {{42}}; auto mem_ptr = &S::x; std::cout << (*vec.begin()).*mem_ptr << '\n'; // This line compiles. std::cout << vec.begin()->*mem_ptr << '\n'; // This line doesn't

c++ friend function - operator overloading istream >>

随声附和 提交于 2019-12-18 04:38:12
问题 My question is in regards to friend functions as well as overloading the << and >>. From my understanding I thought friend functions could (and should) access private member variables directly. However in the case I have here the compiler would only accept my .cxx file when I used "get" functions to obtain each private variable. Here is my header file class BigNum public: // CONSTRUCTORS and DESTRUCTORS BigNum(); BigNum(int num, size_t optional_base = 10); BigNum(const char strin[], size_t