Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Overloading operators

Name: Anonymous 2006-06-09 23:57

What does this mean in english (refering to C++)

Name: Anonymous 2006-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;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List