operator-overloading

How to change the meaning of pointer access operator

Hi All, This may be very obvious question, pardon me if so. I have below code snippet out of my project, #include <stdio.h> class X { public: int i; X() : i(0) {}; }; int main(int argc,char *arv[]) { X *ptr = new X[10]; unsigned index = 5; cout<<ptr[index].i<<endl; return 0; } Question Can I change the meaning of...

overloading -> operator in c++

I saw this code but I couldn't understand what it does: inline S* O::operator->() const { return ses; //ses is a private member of Type S* } so what happens now if I used ->? ...

Is calling of overload operator-> resolved at compile time?

when I tried to compile the code: (note: func and func2 is not typo) struct S { void func2() {} }; class O { public: inline S* operator->() const; private: S* ses; }; inline S* O::operator->() const { return ses; } int main() { O object; object->func(); return 0; } there is a compile error reported: D:\...

Why is T() = T() allowed?

I believe the expression T() creates an rvalue (by the Standard). However, the following code compiles (at least on gcc4.0): class T {}; int main() { T() = T(); } I know technically this is possible because member functions can be invoked on temporaries and the above is just invoking the operator= on the rvalue temporary created...

Function should return reference or object?

Let's discuss these two functions: complex& operator+=(const T& val); complex operator+(const T& val); Where "complex" is a name of a class that implements for example complex variable. So first operator returnes reference in order to be possible to write a+=b+=c ( which is equivalent to b=b+c; a=a+b;). Second operator returnes and...

Why does operator<< not work with something returned by operator-?

Here's a small test program I wrote: #include <iostream> using namespace std; class A { public: int val; A(int _val=0):val(_val) { } A operator+(A &a) { return A(val + a.val); } A operator-(A &a) { return A(val - a.val); } friend ostream& operator<<(ostream &, A &); }; ostream& operator<<(ostream &out, A &...

Why can operator-> be overloaded manually?

Wouldn't it make sense if p->m was just syntactic sugar for (*p).m? Essentially, every operator-> that I have ever written could have been implemented as follows: Foo::Foo* operator->() { return &**this; } Is there any case where I would want p->m to mean something else than (*p).m? ...

delegating into private parts

Sometimes, C++'s notion of privacy just baffles me :-) class Foo { struct Bar; Bar* p; public: Bar* operator->() const { return p; } }; struct Foo::Bar { void baz() { std::cout << "inside baz\n"; } }; int main() { Foo::Bar b; // error: 'struct Foo::Bar' is private within this con...

How do boost operators work?

boost::operators automatically defines operators like + based on manual implementations like += which is very useful. To generate those operators for T, one inherits from boost::operators<T> as shown by the boost example: class MyInt : boost::operators<MyInt> I am familiar with the CRTP pattern, but I fail to see how it works here. Spe...

VB.NET overloading array access?

Hi, Is it possible to overload the array/dict access operators in VB.net? For example, you can state something like: Dim mydict As New Hashtable() mydict.add("Cool guy", "Overloading is dangerous!") mydict("Cool guy") = "Overloading is cool!" And that works just fine. But what I would like to do is be able to say: mydict("Cool guy")...

How to test my application With STL such as vector to make sure whether it works or not in c++?

*we release and the acquire the pointer with the Linked_ptr in parameter */ Linked_ptr& operator=(const Linked_ptr& r) { if (this != &r) { release(); acquire(r); } return *this; } ...

c++ polymorphism of operator overloading

How can I make pure virtual function a operator+(); function. wheh ı do like this in base class int operator+()=0; compiler gives error . in derive class operator+() function compiler say that derive class cannot make . because following class is abstract I know that I cannot create object of abstract classes but now I try to make der...

Overloading stream insertion without violating information hiding?

I'm using yaml-cpp for a project. I want to overload the << and >> operators for some classes, but I'm having an issue grappling with how to "properly" do this. Take the Note class, for example. It's fairly boring: class Note { public: // constructors Note( void ); ~Note( void ); // public accessor methods void ...

Why does '7' + 4 give '74', whereas '7' - 4 gives 3 in javascript?

I read this question in some post on SO, so please explain this. Thanking you. ...

overload == (and != , of course) operator, can I bypass == to determine whether the object is null

Hello, when I try to overload operator == and != in C#, and override Equal as recommended, I found I have no way to distinguish a normal object and null. For example, I defined a class Complex. public static bool operator ==(Complex lhs, Complex rhs) { return lhs.Equals(rhs); } public static bool operator !=(Complex lhs, Complex rh...

Implementing operator< in C++

I have a class with a few numeric fields such as: class Class1 { int a; int b; int c; public: // constructor and so on... bool operator<(const Class1& other) const; }; I need to use objects of this class as a key in an std::map. I therefore implement operator<. What is the simplest implementation of operator< to us...

operator overloading and inheritance

I was given the following code: class FibHeapNode { //... // These all have trivial implementation virtual void operator =(FibHeapNode& RHS); virtual int operator ==(FibHeapNode& RHS); virtual int operator <(FibHeapNode& RHS); }; class Event : public FibHeapNode { // These have nontrivial implementatio...

What is operator<< <> in C++?

I have seen this in a few places, and to confirm I wasn't crazy, I looked for other examples. Apparently this can come in other flavors as well, eg operator+ <>. However, nothing I have seen anywhere mentions what it is, so I thought I'd ask. It's not the easiest thing to google operator<< <>( :-) ...

how to declare type conversion in header file and implement in cpp file?

it doesn't work for me. i have a header file and a cpp file. need to define a conversion operator from my class to INT, but it gives me "syntax error" when declaring it in the H file and implementing in the cpp file. maybe i got the syntax wrong? in the H file i have in "public": operator int(); and in the cpp file i have: A::operato...

Reflection and Operator Overloads in C#

Here's the deal. I've got a program that will load a given assembly, parse through all Types and their Members and compile a TreeView (very similar to old MSDN site) and then build HTML pages for each node in the TreeView. It basically takes a given assembly and allows the user to create their own MSDN-like library for it for documentat...