operator-overloading

Why can't GCC disambiguate multiple inherited functions (yet clang can)? [duplicate]

感情迁移 提交于 2020-01-22 13:43:25
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Why do multiple-inherited functions with same name but different signatures not get treated as overloaded functions? This fails to compile in the indicated place with g++ 4.6.1: enum Ea { Ea0 }; enum Eb { Eb0 }; struct Sa { void operator()(Ea) {} }; struct Sb { void operator()(Eb) {} }; struct Sbroken : Sa, Sb {}; struct Sworks { void operator()(Ea) {} void operator()(Eb) {} }; int main() { Sworks()(Ea0);

Why can't GCC disambiguate multiple inherited functions (yet clang can)? [duplicate]

我的梦境 提交于 2020-01-22 13:42:32
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Why do multiple-inherited functions with same name but different signatures not get treated as overloaded functions? This fails to compile in the indicated place with g++ 4.6.1: enum Ea { Ea0 }; enum Eb { Eb0 }; struct Sa { void operator()(Ea) {} }; struct Sb { void operator()(Eb) {} }; struct Sbroken : Sa, Sb {}; struct Sworks { void operator()(Ea) {} void operator()(Eb) {} }; int main() { Sworks()(Ea0);

Overloading multi-dimensional brackets [duplicate]

折月煮酒 提交于 2020-01-22 02:06:07
问题 This question already has answers here : Operator[][] overload (18 answers) Closed 3 years ago . How can I overload multi-dimensional brackets? Let's say that I have a class which enables me to access points in an n -vector space. For example: class NSpaceVector { private: int vectorSpace[8][8][8][8][8][8][8]; public: const NSpaceVector operator[][][][][][][](int i, int j, int k, int l, int m, int n, int p)const {return vectorSpace[i][j][k][l][m][n][p]; } NSpaceVector operator[][][][][][][]

Why use int as an argument for post-increment operator overload?

半世苍凉 提交于 2020-01-21 12:09:50
问题 As I know here is the way to overload the post-increment operator: const MyClass& MyClass::operator++(int); Why does it have int as an argument? 回答1: D&E, §11.5.3: I conisdered the obvious solution, adding the keywords prefix and postfix to C++ [ ... ] However I received the usual howl of outrage from people who dislike new keywords. Several alternatives that did not involve new keywords were suggested. For example: class Ptr_to_X { X ++operator(); // prefix ++ X operator++(); // postfix ++ }

VB.Net Power operator (^) overloading from C#

空扰寡人 提交于 2020-01-21 06:49:05
问题 I am writing a C# class that is exposed to VB.Net. I would like to overload the vb.net ^ operator so that I can write: Dim c as MyClass Set c = New ... Dim d as MyClass Set d = c^2 In C#, the ^ operator is the xor operator and the power operator doesn't exist. Is there a way I can do this anyway? 回答1: EDIT It turns out there's a SpecialNameAttribute that lets you declare "special" functions in C# that will allow you (among other things) to overload the VB power operator: public class

When are stateless class functors useful in place of a c style function?

若如初见. 提交于 2020-01-21 04:50:12
问题 I've found some good examples of functors on SO like this one, and all the convincing examples seem to use state in the class that defines operator() . I came across an example in a book that defines the function call operator without having state, and I can't help but feel like this is an awkward usage, and that a normal style function pointer, would be better than using operator() in every way here - less code, less variables (you have to instantiate the comparators), its probably more

Why == overloading can access private members of argument [duplicate]

流过昼夜 提交于 2020-01-20 23:01:50
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: why private value of the obj can be changed by class instance? Consider the following (partial) code: class Group { private: int id; public: void set_id(int); int get_id(); bool operator==(const Group&); }; bool Group::operator==(const Group& g) { if(g.id == this->id) { /* id is private? */ return true; } return false; } The code compiles and results seem proper. However, in the if part of the operator

How do I prevent a class from being allocated via the 'new' operator? (I'd like to ensure my RAII class is always allocated on the stack.)

余生颓废 提交于 2020-01-18 07:13:26
问题 I'd like to ensure my RAII class is always allocated on the stack. How do I prevent a class from being allocated via the 'new' operator? 回答1: All you need to do is declare the class' new operator private: class X { private: // Prevent heap allocation void * operator new (size_t); void * operator new[] (size_t); void operator delete (void *); void operator delete[] (void*); // ... // The rest of the implementation for X // ... }; Making 'operator new' private effectively prevents code outside

any working operator overloading example in haskell

戏子无情 提交于 2020-01-17 05:28:21
问题 I want to overload any operator . i want to do such a simple function that for instance think about overloading of == operator .Overload == such that x==y returns x . Or x==y return x+y. It doesn't matter what . Can you show me any simple operator overloading example? I cannot find any example on the web unfortunately. For example;when i call Tree a == Tree a return 5 (it always return 5. I select it ,it is not related to any thing) or when i call 3==4 return : 7 I tried the below codes(i

C++ Dynamically detect class of parameter and cast thereto

旧街凉风 提交于 2020-01-15 09:49:49
问题 I have two classes, one which inherits from the other. The relevant part of the base class is as follows (obviously this class has ctors, a dtor, etc., and particularly an operator[] , but I thought those irrelevant to the matter at hand): #include <array> template < class T, unsigned int N > class Vector { public: template < class U, unsigned int M > friend Vector< U, M > operator+ ( const Vector< U, M >&, const Vector< U, M >& ); template < class U, unsigned int M > friend std::ostream&