operator-overloading

Operator overloading and namespaces [duplicate]

醉酒当歌 提交于 2019-12-29 07:27:07
问题 This question already has answers here : Closed 9 years ago . Possible Duplicate: Where should non-member operator overloads be placed? While browsing on SO, I often find questions or answer that involves overloading/defining a std::ostream& operator<<(std::ostream& os, const Foo& foo) or a Foo operator+(const Foo& l, const Foo& r) . While I know how and when (not) to write these operators, I'm confused about the namespace thing. If I have the following class: namespace bar { class Foo {}; }

needs overload operator< and null check

廉价感情. 提交于 2019-12-29 07:25:08
问题 I´m overloading the lessthan-operator in c# and I`m wondering whether this needs to check for null. Below you can find an Example: public static bool operator <(MyClass x, MyClass y) { if (x == null && y == null) { return false; } if (x == null) { return true; //false? } if (y == null) { return false; //true? } return x.Value < y.Value; } Or is this correct: public static bool operator <(MyClass x, MyClass y) { return x.Value < y.Value; } I didn´t find any instruction on this. But maybe I

Overloading unary operator &

不羁岁月 提交于 2019-12-29 06:39:25
问题 Let's consider a class with overloaded unary operator & (Address-of). Let it be class A template <class C> class A { public: C * operator &() { return &data; } //... private: C data; } Now I want to pass to some function a pointer of type A to fill its data . Let us call it f void f(A * auto_containter) { //... } But it is clear why the code bellow wouldn't work (even wouldn't compile). It is because the overloaded operator is called. A a; f(&a); The question is following: Is there any syntax

About shared_ptr and pointer to member operator `->*` and `std::bind`

醉酒当歌 提交于 2019-12-29 06:17:31
问题 Recently I discovered that shared_ptr does not have pointer to member operator ->* . I created simple example: template <typename Pointer, typename Function, typename... Args> auto invoke1(Pointer p, Function f, Args... args) -> decltype((p->*f)(args...)) { return (p->*f)(args...); } struct A { void g() { std::cout << "A::g()\n"; } }; int main() { A a; invoke1(&a, &A::g); // works!! std::shared_ptr<A> sa = std::make_shared<A>(); invoke1(sa, &A::g); // compile error!! } Q1: Why is so? Why

+ operator for String in Java [duplicate]

风格不统一 提交于 2019-12-28 22:15:14
问题 This question already has answers here : How does the String class override the + operator? (7 answers) Closed 6 years ago . I saw this question a few minutes ago, and decided to take a look in the java String class to check if there was some overloading for the + operator. I couldn't find anything, but I know I can do this String ab = "ab"; String cd = "cd"; String both = ab + cd; //both = "abcd" Where's that implemented? 回答1: From the Fine Manual: The Java language provides special support

+ operator for String in Java [duplicate]

霸气de小男生 提交于 2019-12-28 22:12:11
问题 This question already has answers here : How does the String class override the + operator? (7 answers) Closed 6 years ago . I saw this question a few minutes ago, and decided to take a look in the java String class to check if there was some overloading for the + operator. I couldn't find anything, but I know I can do this String ab = "ab"; String cd = "cd"; String both = ab + cd; //both = "abcd" Where's that implemented? 回答1: From the Fine Manual: The Java language provides special support

How to reduce redundant code when adding new c++0x rvalue reference operator overloads

南楼画角 提交于 2019-12-28 10:05:04
问题 I am adding new operator overloads to take advantage of c++0x rvalue references, and I feel like I'm producing a lot of redundant code. I have a class, tree , that holds a tree of algebraic operations on double values. Here is an example use case: tree x = 1.23; tree y = 8.19; tree z = (x + y)/67.31 - 3.15*y; ... std::cout << z; // prints "(1.23 + 8.19)/67.31 - 3.15*8.19" For each binary operation (like plus), each side can be either an lvalue tree , rvalue tree , or double . This results in

How to overload operator==() for a pointer to the class?

不羁岁月 提交于 2019-12-28 06:46:07
问题 I have a class called AString . It is pretty basic: class AString { public: AString(const char *pSetString = NULL); ~AString(); bool operator==(const AString &pSetString); ... protected: char *pData; int iDataSize; } Now I want to write code like this: AString *myString = new AString("foo"); if (myString == "bar") { /* and so on... */ } However, the existing comparison operator only supports if (*myString == "bar") If I omit that asterisk, the compiler is unhappy. Is there a way to allow the

Why does Python have an __ne__ operator method instead of just __eq__?

时间秒杀一切 提交于 2019-12-28 05:36:09
问题 The answer here gives a handwaving reference to cases where you'd want __ne__ to return something other than just the logical inverse of __eq__ , but I can't imagine any such case. Any examples? 回答1: SQLAlchemy is a great example. For the uninitiated, SQLAlchemy is a ORM and uses Python expression to generate SQL statements. In a expression such as meta.Session.query(model.Theme).filter(model.Theme.id == model.Vote.post_id) the model.Theme.id == model.VoteWarn.post_id does not return a

Undefined symbol on a template operator overloading function

我的梦境 提交于 2019-12-28 03:13:29
问题 I have this function declaration: template<class T> a::A& a::A::operator<<(T out) { std::cout << out; return (*this); } and this function definition: namespace a { ... class A { ... template<class T> A& operator<<(T); And I call it as: a::A b; b << 1; and this is the Makefile: app: main.o A.o g++ main.o A.o -o app main.o: main.cpp g++ -c main.cpp A.o: A.cpp g++ -c A.cpp and it gives me: Undefined symbols: a::A& a::A::operator<< <int>(int) why is that? 回答1: The function template will be turned