an example of the equals operator being overloaded would be awesome.
Name:
Anonymous2006-06-10 0:53
well until someone else more knowledgable posts, i'll try to give you a bit of insight. basically let's say you have a BigInteger class and you want to add two BigIntegers together. you could write a method (ie. public BigInteger add(BigInteger a, BigInteger b) ), however you can also do that much more transparently with operator overloading. By creating a new definition for the '+' (plus sign) to add two BigIntegers--with your logic attached to how the BigIntegers should be added. Thus, you can more concisely write: BigInteger c = a + b; with the overloaded '+' operator. I don't know C++ well at all, but i believe you can also overload the other operators such as '-', '/', '*', '++', '~', etc.
Like i said before, someone else who has a better knowledge of C++ can probably explain it better. HTH
Name:
Anonymous2006-06-10 2:22
You have to first understand that all operators in C++, or any language, could have been designed as function calls. For example (assume all numbers are int types)
F = 2 + 7 could be: F = add(2,7) or even: equals(F,add(2,7))
Of course we like operators and no one would seriously design a language like that.
Overloading an operator lets you put your own function for the action that an operator takes instead of the built in action.
The real reason why C++ allows this: C++ knows what to do with the operators with respect to built in types. It knows that int + int means to add two ints and produce a third int. However, when you make your own types, C++ does not automatically know what the operators "should" mean and that's why you have to overload them.
Incidentally, you can't overload built in types like int, float, etc. so ignore the implication I just made that you could.
Name:
Anonymous2006-06-10 2:38
>>1
an overloaded operator is just a fancy method in an object that gets called when said operator is used in a program. thus: SomeObject a;
a = 5;
a = a + 5;
Name:
Anonymous2006-06-10 3:28
>>2
Well I know how to do it in C++ if that's cool: #include <iostream>
// In this implementation the numbers will not scale, for simplicity of the example
// and because it's fucking late here.
template <typename T>
class Number {
T *m_value;
public:
const T &value; // a reference to internal data
Number (T val_in = 0) : m_value(new T(val_in)), value(*m_value) { }
~Number() { delete m_value; }
Number operator+ (const Number &rhs) {
return Number(value + rhs.value);
}
// asignment operator for Numbers
Number &operator= (const Number &rhs) {
if (this == &rhs)
// if this is true, we attempted to assign an object to itself.
return *this;
delete m_value; // but if it isn't the same object, delete the old data
m_value = new T(rhs.value); // and replace it with new data
return *this;
}
};
int main() {
Number<double> a = 2.2e6, b = 4.2933e3;
Number<double> c = b + a;
std::cout << a.value << " " << b.value << " " << c.value;
return 0;
}
>>6
GAME OVER!!! If you try to add 2 objects, your program will fail like GWB's war in Irak. Replace in the destructor: { delete m_value; }
Name:
Anonymous2006-06-14 12:29
This is >>6. I'm not sure what you meant or tried to do to my program but it won't add Number<int>s to Number<double>s or anything like that -- like I told you in the comment: Numbers will not scale. The templates would need some work in order to do that, and I wrote that at 3:30 am.
My destructor is fine. I didn't declare any array space on the heap so delete[] has no reason to be there.
The output works as it should.
Name:
Anonymous2006-06-14 17:32
>>6,10,11 Nevermind my idiocy... I should have just wrote this better, period. Here's something: #include <iostream>
template <typename T> class Number {
T *m_value;
public:
Number(T val_in = 0) : m_value(new T(val_in)) { }
~Number() { delete m_value; }
Number operator+ (const Number &rhs) {
return Number(*m_value + *(rhs.m_value));
}
Number &operator= (const Number &rhs) {
if (this == &rhs)
return *this;
delete m_value;
m_value = new T(*(rhs.m_value));
return *this;
}
Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy