operator-overloading

How can I return a value from a class object?

Is it possible to have a class object return a true/false value, so I can do something like this: MyClass a; ... if (a) do_something(); I can accomplish (almost) what I want by overloading the ! operator: class MyClass { ... bool operator!() const { return !some_condition; }; ... } main() MyClass a; ... i...

How do I create overloaded operators for boost pointers in C++?

From this post I see that you can't overload operators for pointers: http://stackoverflow.com/questions/1419997/c-operator-overloading-of-for-pointers-to-objects But is there any way I could overload operators for boost pointers? For example: boost::shared_ptr<ClassB> operator||(boost::shared_ptr<ClassA> lhs, boost::shared_ptr<ClassA...

F#: Why am I required to supply parameter types when overloading member functions?

Given the following code: type MyType() = static member processString (_string:string) = _string.Substring(0, 1) static member processInt (_int:int) = _int.ToString() static member processItems = List.map MyType.processString static member processItems = List.map MyType.processInt The last two lines will not work. I h...

error: pointer being freed was not allocated

I am trying to overload the assignment operator to do a deep copy of a polygon object, the program compiles but I am getting an error toward the end that I want to clear up. Below is the relevant code, if you think I need to add more please just post a comment. Assume the proper #include's and that the << operator is overloaded for prope...

Overloading overloaded operator=?

What can I do when I need different job done, depending on the rval and lval type? Defining several overloads pop out with error 'operator = is ambiguous'. Any ideas, or tips (links to tutorials), are much appreciated, since I've just today found out about operator overloading. Thanks in advance! EDIT: Yes, I'd use C++0x since I have...

How can I index a collection and allow each index's variable to have a custom setter?

So I have an object called Vertex which contains some parameters (let's call them sx, sy and i). sx, sy and i each have special setters: ie, Vertex looks something like class Vertex { public: float sx() { return sx; }; void setSx(float val) { val > 0 ? sx = val : sx = 0; }; float sy() {...

Method Overloading in Objective-C - not used for init?

I have just started programming in Objective-C, I understand it only partially supports method overloading due to the way the method names are generated (see this question). However, my question is why I have never seen it used in any examples. The code below seems to work fine, but any sort of example I have seen, the second init would...

Using a (mathematical) vector in a std::map

Related: what can I use as std::map keys? I needed to create a mapping where specific key locations in space map to lists of objects. std::map seemed the way to do it. So I'm keying a std::map on an xyz Vector class Vector { float x,y,z } ; , and I'm making a std::map<Vector, std::vector<Object*> >. So note the key here is not ...

Why is this ambiguity here?

Consider I have the following minimal code: #include <boost/type_traits.hpp> template<typename ptr_t> struct TData { typedef typename boost::remove_extent<ptr_t>::type value_type; ptr_t data; value_type & operator [] ( size_t id ) { return data[id]; } operator ptr_t & () { return data; } }; int main( int argc, char **...

Overload += operator if + is overloaded?

Possible Duplicate: Overloading += in c++ Do I need to overload the += operator if I overload + or will the compiler know what to do? Thanks. ...

Struggling to get '==' operator overloading to work (C++).

Okay, not sure what I'm doing here, other than it's not right. Trying to overload the '==' method of a class, and it's just... not working. At least, I get a false back from my main, and the cout in the implementation of '==' doesnt output. These are my three files: // TestClass.h #ifndef TESTCLASS_H #define TESTCLASS_H class TestCla...

Why doesn't Java need Operator Overloading ?

Why doesn't Java need operator overloading? Is there any way it can be supported in Java? ...

How to do Operator Overloading?

I did this : let (-) (m:float[]) (n:float[])= [| for i = 0 to Array.length m - 1 do yield m.[i]-n.[i] |] But, why this is wrong?! let y=1.0-0.0 That is ok before! Error 1 This expression was expected to have type float [] but here has type float E:\MyDocuments\Visual Studio 2010\Projects\curve intersection\ne...

Why does the postfix increment operator take a dummy parameter?

Have a look at these function signatures: class Number { public: Number& operator++ (); // prefix ++ Number operator++ (int); // postfix ++ }; Prefix doesn't take any parameter but postfix does. Why? I thought we can recognize them with different return types. ...

Overloading the C++ indexing subscript operator [] in a manner that allows for responses to updates

Consider the task of writing an indexable class which automatically synchronizes its state with some external data-store (e.g. a file). In order to do this the class would need to be made aware of changes to the indexed value which might occur. Unfortunately the usual approach to overloading operator[] does not allow for this, for exampl...

binary '*' : no global operator found which takes type 'statistician' (or there is no acceptable conversion)

I am trying to overload my operators its really just a class that holds arithmetic functions and a sequence of array variables. But when i am overloading my (*) multiplication operator i get this error: binary '*' : no global operator found which takes type 'statistician' (or there is no acceptable conversion) This happens when...

overloading operator+ (C++)

Hey so I am working on an overload of +, but when I try to do a simple call like statistician a,b,c; a = b+c; When I do the above call it crashes. and if i do an overload of =, it just returns a zero. here is some of my code. Thanks for the help guys!!! FYI next_number(double value), increases the dynamic array by 1, and puts that ...

Operator Overloading with Chaining

I would like to overload << operator for chaining like below function1(param1, param2)<< obj2 << obj3 << string4 function1 would return an object. What I want to do is after string4, I need to call a function using param1, param2. My questions will be How do I know string4 is the last parameters in the expression and hence I need...

Is this a safe way to implement a generic operator== and operator<?

After seeing this question, my first thought was that it'd be trivial to define generic equivalence and relational operators: #include <cstring> template<class T> bool operator==(const T& a, const T& b) { return std::memcmp(&a, &b, sizeof(T)) == 0; } template<class T> bool operator<(const T& a, const T& b) { return std::mem...

Can I overload an operator in Objective-C?

Is it possible to override operator use in Objective-C? For example myClassInstance + myClassInstance calls a custom function to add the two. ...