is it ok to use *this in that case?Code:void GetItem()
{
for (int iii=0; iii!=MAX_INV; iii++)
if (charInv[iii].id==0){ charInv[iii] = *this); break;}
else if (charInv[iii].id==id)
if (amount < stackSize){ amount++; break;}}
Printable View
is it ok to use *this in that case?Code:void GetItem()
{
for (int iii=0; iii!=MAX_INV; iii++)
if (charInv[iii].id==0){ charInv[iii] = *this); break;}
else if (charInv[iii].id==id)
if (amount < stackSize){ amount++; break;}}
It will make a copy of the current object, so sure.
You could also store a pointer to the object, but then you must beware of dangling pointer issues if the object goes out of scope.
Your indentation needs work, too, if I may say so (too many spaces/tabs for indentation).
Especially, you should put the ending } on a separate line. It makes it much more readable.
say, does it make any sense?Code:void AddItem(int A)
{
int B;
amount += A;
if( amount < stackSize )
{
B = amount - stackSize;
GetItem(B)
}
}
void GetItem(int A)
{
for( int iii=0; iii != MAX_INV; iii++ )
if( charInv[iii].id == 0 )
{
charInv[iii] = *this;
charInv[iii].additem(A);
break;
}
else if( charInv[iii].id == id )
if( charInv[iii].amount < stackSize )
{
charInv[iii].AddItem(A);
break;
}
}
You mix tabs and spaces. Don't.
Besides that one would expect a method called GetItem, to fetch and return an item, not add A to elements of some array called charInv.
Also C++ is case sensative so AddItem and additem are two different methods, or broken code if the latter method doesn't exist.