Yes, it is possible to overload (not override) operators. However, the problem is, you are not doing it correctly.
You are trying to invoke operator-
on Resurce*
pointers, not on Resurce
objects. Your operator-
needs to take a Resurce
object by value/reference as input, not by pointer. And return a new Resurce
object by value on output, not by pointer. See What are the basic rules and idioms for operator overloading?
And in your main()
, you need to dereference the pointers before calling operator-
.
Try this:
Resurse operator-(const Resurse &r) const
{
return Resurse(_cantitate - r._cantitate);
}
int main()
{
Resurse* hrana1 = new Resurse(20);
Resurse* hrana2 = new Resurse(17);
Resurse result = *hrana1 - *hrana2;
std::system("pause");
delete hrana2;
delete hrana1;
}
Or, simply get rid of the pointers altogether, you don't really need them:
int main()
{
Resurse hrana1(20);
Resurse hrana2(17);
Resurse result = hrana1 - hrana2;
std::system("pause");
}
UPDATE: I just realized that you are trying to implement a polymorphic operator-
. That is not going to work, mainly due to object slicing on the return value, but also because you can't overload operators for pointers.
I would suggest using a separate virtual method instead of operator-
, eg:
#pragma once
#include <memory>
class Resurse
{
protected:
unsigned int _cantitate;
public:
Resurse(unsigned int cantitate = 0) : _cantitate(cantitate) {}
virtual ~Resurse() {}
Resurse(const Resurse&) = default;
Resurse(Resurse&& r)
{
_cantitate = r._cantitate;
r._cantitate = 0;
}
virtual std::unique_ptr<Resurse> subtract(const Resurse &r) const
{
return std::make_unique<Resurse>(_cantitate - r.GetCantitate());
}
unsigned int GetCantitate() const { return _cantitate; }
};
#pragma once
#include "Resurse.h"
#include <memory>
#include <cstdlib>
class Hrana : public Resurse
{
public:
Hrana(unsigned int cantitate = 0) : Resurse(cantitate) {}
Hrana(const Hrana& h) = default;
Hrana(Hrana&& h) : Resurse(std::move(h)) {}
std::unique_ptr<Resurse> subtract(const Resurse &r) const override
{
return std::make_unique<Hrana>(_cantitate - r.GetCantitate());
}
};
void main()
{
std::unique_ptr<Resurse> hrana1 = std::make_unique<Hrana>(20);
std::unique_ptr<Resurse> hrana2 = std::make_unique<Hrana>(17);
auto result = hrana1->subtract(*hrana2);
std::system("pause");
}