I need help on finding the problem using a custom c++ class to manage 3D positions. Here is the relevant code from the class
Punto operator+(Punto p){
return Punto(this->x + p.x, this->y + p.y, this->z + p.z);
}
Punto operator+(Punto *p){
return Punto(this->x + p->x, this->y + p->y, this->z + p->z);
}
Punto operator-...
Hi,
Just wondering if there is anyway to represent the following code in C# 3.5:
public struct Foo<T> {
public Foo(T item) {
this.Item = item;
}
public T Item { get; set; }
public static explicit operator Foo<U> ( Foo<T> a )
where U : T {
return new Foo<U>((U)a.Item)
}
}
Thanks
...
First of all I apologize for the long lead up to such a simplistic question.
I am implementing a class which serves as a very long 1 dimensional index on a space filling curve or the n-tuple representing the Cartesian coordinate that index corresponds to.
class curvePoint
{
public:
friend class curveCalculate;
//Construction a...
Hello.
I overloaded the == operator on my class as follows:
public static bool operator ==(Table pt1, Table pt2) {
return Compare(pt1, pt2) == 0 && pt1.TableName == pt2.TableName;
}
Compare will work just as the strcmp does in c++, returning an integer. Problem is that if I do an if (MY_CLASS == null), it will call my == operator...
I wrote an abstraction class for a math object, and defined all of the operators. While using it, I came across:
Fixed f1 = 5.0f - f3;
I have only two subtraction operators defined:
inline const Fixed operator - () const;
inline const Fixed operator - (float f) const;
I get what is wrong here - addition is swappable (1 + 2 == 2 + ...
Python 2.x has two ways to overload comparison operators, __cmp__ or the "rich comparison operators" such as __lt__. The rich comparison overloads are said to be preferred, but why is this so?
Rich comparison operators are simpler to implement each, but you must implement several of them with nearly identical logic. However, if you ca...
I'm building a tree-based data structure and overloaded [ ] so that I can say
node["key1", "key2", "key3"]
which returns the node whose parents 1, 2, and 3 levels above are the nodes with those keys. the nodes conceptually map to an array of data, so what I have now is this function:
node[keys...].SetValue(i, value)
which sets the ...
Hi.
Is there a way of forcing the C# compiler to ignore missing operator overloads for objects, and instead handle that check in runtime? I ask because I have a container that has multiple objects that has various attributes of type int, string, ushort and so on. I'm making a search function for that container, and would like to be able...
Hi!
Let's say I have a class Point:
class Point {
int x, y;
public:
Point& operator+=(const Point &p) { x=p.x; y=p.y; return *this; }
};
Why can I not call this as such:
Point p1;
p1 += Point(10,10);
And is there any way to do this, while still having a reference as the argument?
...
If I've overloaded operator+ and operator= do I still need to overload
operator+= for something like this to work: -?-
MyClass mc1, mc2;
mc1 += mc2;
...
Operator overloading in C++ is considered by many to be A Bad Thing(tm), and a mistake not to be repeated in newer languages. Certainly, it was one feature specifically dropped when designing Java.
Now that I've started reading up on Scala, I find that it has what looks very much like operator overloading (although technically it doesn'...
In C++, can you have a templated operator on a class? Like so:
class MyClass {
public:
template<class T>
T operator()() { /* return some T */ };
}
This actually seems to compile just fine, but the confusion comes in how one would use it:
MyClass c;
int i = c<int>(); // This doesn't work
int i = (int)c(); // Neither does this*...
C++ does not allow polymorphism for methods based on their return type. However, when overloading an operator this seems possible.
Does anyone know why? I thought operators are handled like methods internally.
Edit: Here's an example:
struct func {
operator string() { return "1";}
operator int() { return 2; }
};
int main( ) {...
I intend to call a function whenever m_logger<<"hello"<<"world" is called. m_logger is of type ofstream.
So i decide to overload << with following signature
friend ofstream& operator<<(ofstream &stream,char *str);
However the vc compiler gives following error:
error C2666: 'operator <<' : 6 overloads have similar conversions
Is...
I have a CCounter class which holds and integer value protected by mutex. I've defined several operators like post/pre inc/dec returning an integer so I can do:
CCounter c(10);
int i = c++;
but what do I do with a simple assignment like i = c ? I tried to define friend operator= but it gives me
operator=(int&, const CCounter&)’ must ...
Is there a way to overload the event += and -= operators in C#? What I want to do is take an event listener and register it to different events. So something like this:
SomeEvent += new Event(EventMethod);
Then instead of attaching to SomeEvent, it actually attaches to different events:
DifferentEvent += (the listener above);
Anot...
The operators are = () [] -> ->* conversion operators
These can be declared only as member functions.
Any other operator function can be either a class member or a non-member function.
What is the rationale for this restriction?
...
Thanks to everyone in advance -
alert((~1).toString(2));
outputs: -10
but in PHP/Java it outputs 11111111111111111111111111111110
Am I missing something, why does Javascript add a "-" to the output?
Thx,
Sam
...
any system defined user type past to ostream object is converted to a string or char* ?
like cout<<4<<"Hello World";
works perfectly fine, how is this achieved? is the << operator overloaded for each and every type? is there a way to achieve it through just one generic overloaded function? what i mean is can i have just one overloaded...
I overloaded operator <<
template <Typename T>
UIStream& operator<<(const T);
UIStream my_stream;
my_stream << 10 << " heads";
Works but:
my_stream << endl;
Gives compilation error:
error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'UIStream' (or there is no acceptable conversion)
What is ...