how do i return results when using operator overloading?

I see several issues with your code:

  • your default constructor is not initializing data.

  • your operator+ is not attempting to copy values from this->data and obj.data into temp.data, thus leaving temp.data uninitialized.

  • your operator+ is returning the wrong MyClass object. It goes to the trouble of preparing a MyClass object named temp, and then completely discards temp upon exit. By passing temp.size to return, you are creating another MyClass object via your MyClass(int size) constructor, which generates all new random data. You need to instead return the temp object that you prepared. Then the compiler will either call your copy constructor to assign temp to c in main(), or it will optimize away the copy to let operator+ operate directly on c.

  • you are missing a copy assignment operator, per the Rule of 3. Nothing in your example actually invokes a copy assignment, but you need to implement the operator properly nonetheless. And if you are using C++11 or later, you should also follow the Rule of 5 as well, by adding a move constructor and a move assignment operator. However, if you can change your design to use std::vector instead of new[], then you can follow the Rule of 0 and let the compiler do all the hard work for you.

With that said, try something more like this:

#include <iostream>
#include <algorithm>
#include <utility> // C++11 and later only...
#include <cstdlib>
#include <ctime>    

class MyClass
{
private:
    int* data;
    size_t size;
    
public:
    MyClass(size_t size = 0) : data(NULL), size(size)
    {
        if (size > 0)
        {
            data = new int[size];
            // in C++11 and later, consider using std::uniform_int_distribution instead of rand()!
            std::generate(data, data + size, std::rand);
        }
    }

    MyClass(const MyClass& obj) : data(NULL), size(obj.size)
    {
        if (size > 0)
        {
            data = new int[size];
            std::copy(obj.data, obj.data + obj.size, data);
        }
    }

    // C++11 and later only...
    MyClass(MyClass&& obj) : data(NULL), size(0)
    {
        std::swap(size, obj.size);
        std::swap(data, obj.data);
    }

    ~MyClass()
    {
        delete[] data;
    }

    MyClass& operator=(const MyClass& rhs)
    {
        if (&rhs != this)
        {
            MyClass temp(rhs);
            std::swap(size, temp.size);
            std::swap(data, temp.data);
        }
        return *this;
    }
    
    MyClass& operator=(MyClass&& rhs)
    {
        MyClass temp(std::move(rhs));
        std::swap(size, temp.size);
        std::swap(data, temp.data);
        return *this;
    }

    MyClass operator+(const MyClass& obj) const
    {
        MyClass temp;
        temp.size = size + obj.size;
        if (temp.size > 0)
        {
            temp.data = new int[temp.size];
            std::copy(data, data + size, temp.data);
            std::copy(obj.data, obj.data + obj.size, temp.data + size);
        }
        return temp;
    }

    friend std::ostream& operator<<(std::ostream& os, const MyClass& obj);
};

std::ostream& operator<<(std::ostream& os, const MyClass& obj)
{
    os << obj.size;
    for(size_t i = 0; i < obj.size; ++i)
    {
        os << " " << obj.data[i];
    }
    return os;
}

int main()
{
    std::srand(std::time(0));
    MyClass a(5);
    MyClass b(a);
    MyClass c = a + b;
    std::cout << c;
    return 0;
}

Which can be simplified to this:

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

class MyClass
{
private:
    std::vector<int> data;
    
public:
    MyClass(size_t size = 0) : data(size)
    {
        std::generate(data.begin(), data.end(), std::rand);
    }

    MyClass operator+(const MyClass& obj) const
    {
        MyClass temp;
        if (!data.empty() || !obj.data.empty())
        {
            temp.data.reserve(data.size() + obj.data.size());
            temp.data.insert(temp.data.end(), data.begin(), data.end());
            temp.data.insert(temp.data.end(), obj.data.begin(), obj.data.end());
        }
        return temp;
    }

    friend std::ostream& operator<<(std::ostream& os, const MyClass& obj);
};

std::ostream& operator<<(std::ostream& os, const MyClass& obj)
{
    os << obj.size;
    for(size_t i = 0; i < obj.size; ++i)
    {
        os << " " << obj.data[i];
    }
    return os;
}

int main()
{
    std::srand(std::time(0));
    MyClass a(5);
    MyClass b(a);
    MyClass c = a + b;
    std::cout << c;
    return 0;
}

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top