operator-overloading

c++ input stream not waiting for input with extraction operator overload

This problem is annoying me. Instead of waiting for input, it just closes. I've been trying to figure this out for a while now. Any ideas? istream& operator>>(istream& is, Account &a) { cout << "Enter an accoutn number: "; is >> a.accountNo; cout << endl; cout << "Balance: "; is >> a.bal; retur...

How do you override operator == when using interfaces instead of actual types?

I have some code like this: How should I implement the operator == so that it will be called when the variables are of interface IMyClass? public class MyClass : IMyClass { public static bool operator ==(MyClass a, MyClass b) { if (ReferenceEquals(a, b)) return true; if ((Object)a == null || (Object...

Implicit array casting in C#

Hi, I have the following classes with an implicit cast operator defined: class A { ... } class B { private A m_a; public B(A a) { this.m_a = a; } public static implicit operator B(A a) { return new B(a); } } Now, I can implicitly cast A to B. But why can't I implicitly cast A[] to B[...

ActiveRecord :has_many associations and === operator

Can someone explain me this Ruby on Rails puzzle? class Post < ActiveRecord::Base has_many :comments end Post.first.comments.class => Array Array === Post.first.comments => false Array === [ 1 ] => true ...

operator << overload c++

how can i overload "<<" operator (for cout) so i could do "cout" to a class k ...

difference between (++i) and (i++)

I understand that (++i) should return a reference to i because the need of concatenation of operators, but what I can't figure out is: Why (i++) should return i by value? can anyone please clarify. Thank you *edit: i forgot to mention that i mean c++ ...

enable_if and conversion operator?

Any chance to use enable_if with a type conversion operator? Seems tricky, since both return type and parameters list are implicit. ...

Any way to determine if class implements operator()

I'm trying to find is there's a way to check if a class is a functional because i want to write a template which uses it? Is there an easy way to do this? Or do I just wrap things in a try/catch? Or perhaps the compiler won't even let me do it? ...

What would the problem be with this assignment operator overload

My program crashes when it tries to assign one object to the other, do you guy see anything wrong with this? The variables are: Field *fields[50]; int numOfFields; int currentField; The function is: Screen& operator=(Screen &scr) { if (this != &scr){ for (int i = 0; i < 50; i++) fields[i] = NULL; for (int i = 0; i <...

assignment operator overloading problem

This issue has me confused. The first piece of code works fine without crashing, it assigns s1 to s2 perfectly fine. But the second group of code causes the program to crash. Anyone have any idea on why this is happening or what the problem could be? Code 1:(works) s1.add(10, 30, 25, "Test Screen", false); s1.add(13, 10, 5, "Nam...

Can C++ assignment operators be free functions?

I'm trying something like this: Foo & operator=(Foo & to, const Bar &from); But I'm getting this error: E2239 'operator =(Foo &, const Bar &)' must be a member function Are there limitations on which operators can/cannot be defined as Free Functions, and if so, why? ...

std::map::find().

Hi, I have a simple struct which i'm using as a key in a std::map struct PpointKey{ unsigned int xp,yp; //pixel coordinates unsigned int side; PpointKey(unsigned xp,unsigned yp,unsigned side=5):xp(xp),yp(yp),side(side) {} bool operator==(const PpointKey& other) const{ const ...

How to reuse enum operator overloads in C++?

I have several flag-like enumerations, in C++. For example: enum some_state { state_normal = 1 << 0, state_special = 1 << 1, state_somethingelse = 1 << 2, state_none = 0, }; some_state var1; Now on using bit operators like & or |, I get compilers errors. I know I can overload operator | et.al. for enums, ...

Fast vector struct that allows [i] and .xyz-operations in D?

I'd like to create a vector struct in D that works like this: vec u, v; vec w = [2,6,8]; v.x = 9; // Sets x v[1] = w.y; // Sets y u = v; // Should copy data Later I'd also like to add stuff like u = v * u etc. But the above will do for now. This is how far I've come: struct vec3f { float[3] data; alias data this; @propert...

C++ Typedefs and operator overloading

If you define a type like typedef int MY_INT; and go on to overload, say, the adition operator of MY_INT like MY_INT operator+(MY_INT a, MY_INT b); will MY_INT a, b; a + b; be different from int A, B; A + B; ? Sorry for any syntax errors. I'm not near a compiler and I want to ask this before I forget about it. ...

C# Implicit operators and ToString()

Hello, I'm creating my own type for representing css values (like pixels eg. 12px ). To be able to add/subtract/multiply/... my type and ints I've defined two implicit operators to and from int. Everything works great except one thing.. If I write: CssUnitBase c1 = 10; Console.WriteLine(c1); I get "10" instead of "10px" - implicit co...

Why must the copy assignment operator return a reference/const reference?

In C++, the concept of returning reference from the copy assignment operator is unclear to me. Why can't the copy assignment operator return a copy of the new object? In addition, if I have class A, and the following: A a1(param); A a2 = a1; A a3; a3 = a2; //<--- this is the problematic line The operator= is defined as follows: A A:...

How can i override a base classes == operator, so the override gets called

With code like the following public class Task { string Name; public static bool operator ==(Task t1, Task t2) { return t1.Name = t2.Name && t1.GetType() == t2.GetType(); } } public class TaskA : Task { int aThing; public static bool operator ==(TaskA t1, TaskA t2) { return (Task)t1 == (Task)t2 && t1.GetType() == t...

C++ multiple operator overloads for the same operator

I know I can answer this question easily for myself by generatin the code and see if it compiles. But since I couldn't find a similar question, I thought it's knowledge worth sharing. Say I am overloading the + operator for MyClass. Can I overload it multiple times. Different overload for different types. Like this: class MyClass{ ... ...

How can I map a remote device as an array in C++?

I would like to create a C++ object to wrap the RAM of an external peripheral device. I'm trying to set up something like the following: Peripheral p; p[4] = 10; int n = p[5]; To do this I need to read or write to the peripheral whenever an array element is accessed. I cannot work out how to do this using operator overloading etc. I ...