Returning an object with a dynamic pointer
If I have an class with a dynamically allocated array inside of it, how can I return an object from an overloaded addition operator?
The class is basically:
Code:
class foo
{
foo& operator + (foo & myFoo);
int *myints;
};
Assume that all myints pointers are dynamically allocated to being 10 integers, which have values...
Then in the addition I tried
Code:
foo& foo::operator+ (foo & myFoo)
{
foo mytemp; //assume mytemp's myints also is correctly allocated as per above...
for (int a=0; a < 10; a++)
{ *(temp.myints + a) = *(myFoo.myints + a) + *(this->myints + a);
}
return temp;
}
When I try code like this (sorry I don't have the exact code-- I'm on a my laptop at the moment...) I get a warning that I'm returning a reference to a local variable. Is there any way to fix this??
thanks! :)