Hello,

does anyone know how to unwrap an Object? for example z and y are of type Element and 5 is a primitive double. I can write an operator+ function to add the two object together ie (z+y) but I don't knw what to do to add 5+z, Z is immutable and can therefore not be typecasted to be of another type. Please help. Everything I have tried failed. The code I wrote to perform addition between 2 objects can be seen below.

Element& Element::operator+(const Element &obj) const
    {
      Element *r = new Element(number + obj.number); /*number is an instance variable of type double in my Element Class*/
      return *r;
    }

Recommended Answers

All 3 Replies

You can write a few non-member friend operators with arguments covering the different cases:

class Element {
    double number;
public:
    Element(double init): number(init) {}

    friend Element operator+(const Element& lhs, double rhs);
    friend Element operator+(double lhs, const Element& rhs);
    friend Element operator+(const Element& lhs, const Element& rhs);
};

Element operator+(const Element& lhs, double rhs)
{
    return Element(lhs.number + rhs);
}
 
Element operator+(double lhs, const Element& rhs)
{
    return Element(lhs + rhs.number);
}
 
Element operator+(const Element& lhs, const Element& rhs)
{
    return Element(lhs.number + rhs.number);
}

By the way, allocating memory with new and then returning the dereferenced object is just begging for a memory leak.

commented: Thank you :-) as for my memory leaks, will take care of it and check them using Valgrind. +1

I started from scratch and followed ur guidelines, even made some friend functions... after a few bug fixes my program compiled beautifully without errors, until i ran it and got a segmentation fault. I suspect its because i was allocating memory within the functions? i thought creating a pointer member variable would solve this as i deallocate the memory in the destructor. I really am not sure what I am doing incorrectly. my code can be found below:

#include "Element.h"
#include <iostream>
using namespace std;

//constructors
    Element::Element()
    {
      result = new Element[1];
      number = 0.0;
    }
    
    Element::Element(double d)
    {
      result = new Element[(int)d];
      number = d;
    }
    
    Element::Element(Element& obj)
    {
      result = new Element[100];
      number = obj.number;
    }
    
    //destructor
    Element::~Element()
    {
      delete [] result;
    }
    
      /** Friend Functions*/
    //operator to allow out of object using output string
    ostream &operator<<(ostream& output, const Element& obj)
    {
      output<<obj.number;
      return output;
    }
    
    //operators to allow double + object 
    Element operator+(const Element& lhs, double rhs)
    {
      Element *frndResult = new Element(lhs.number + rhs);
      
      return *frndResult;
    }
    
    Element operator+(double lhs, const Element& rhs)
    {
      Element *frndResult = new Element(lhs + rhs.number);
      
      return *frndResult;
    }
    
//     Element operator+(const Element& lhs, const Element& rhs)
//     {
//       Element *frndResult = new Element(lhs.number + rhs.number);
//       
//       return *frndResult;
//     }
    //operators to allow object + double
    
    /** End of friend Functions*/
    
    Element& Element::operator=(const Element& rhs)
    {
      this->number = rhs.number;
      
      return *this;
    }
    
    Element& Element::operator+(const Element& rhs) const
    {
      Element *result1 = new Element(number + rhs.number);
      
      return *result1;
    }
    
    Element& Element::operator-(const Element& rhs)
    {
      Element *result1 = new Element(number - rhs.number);
      
      return *result1;
    }
    
    Element& Element::operator/(const Element& rhs)
    {
      Element *result1 = new Element(number / rhs.number);
      
      return *result1;
    }
    
    Element& Element::operator*(const Element& rhs)
    {
      Element *result1 = new Element(number * rhs.number);
      
      return *result1;
    }

I managed to solve it :-D the pointer member variable was not necessary so i commented it out, together with all its instances of which i was allocating memory. My destructor is now empty... yes I may have some memory leaks but at least for now i have solved the problem. I hope that as i continue with programming assignments, I'll code better and avoid memory leaks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.