I'm training my C++ and I'm trying to write a library that'll be able to represent the following number using linked lists:
999999999 * ( [i=0]Σ[999999999] 1000000000 ^ i )
For example if my number was 711381450277869054011, it'll be represented like this:
711 * 1000000000^2 + 381450277 * 1000000000^1 + 869054011 * 1000000000^0
So, here is the struct of my LL and it's functions:
typedef struct node* ll;
struct node
{
unsigned int data;
ll next;
};
bool insert(ll&, unsigned int);
// ...
void copy(ll&, ll);
void destroy(ll&);
And here is my class of unsigned very long integers:
class uli
{
public:
uli();
~uli();
// <<, >>, =, operators...
uli& operator +=(const uli&);
uli& operator +=(char*);
const uli operator +(const uli&);
private:
ll head;
};
uli::uli()
{
head = NULL;
insert(head, 0);
}
uli::~uli()
{
destroy(head);
}
+= operator works fine and I'm using it to overload the + operator.
The problem is that I can't make the method operator+() return a const uli without it being destroyed by the deconstructor before I get to use it.
const uli uli::operator +(const uli& rarg) // rarg: right argument
{
uli larg; // left argument
larg = *this;
larg += rarg;
return larg;
}
// returns an LL to somewhere ???? larg was destroyed.
Now I can't do Z = X + Y;