Why not just do this:
Code:
CProduct* pToProduct[10];
since your product array is always 10 anyway
As for comparing, if your CProduct's _id is public you can just compare it directly. If it's not just give it something like:
Code:
//note this returns const reference - important for efficiency!
const myString& GetID() const
{
return _id;
}
And use that in your sort like this:
Code:
//referencing the pointer to the product using ->, 'x' is a random element you want to get
pToProduct[x]->_id; //only if _id is public, OR:
pToProduct[x]->GetID(); //probably much safer
Now you have all you need to do the sorting. If you are required to fill out your array of pointers first, bubble sort sounds like a good choice here. Google it, it's pretty simple.
Also, there are a few sort functions in the standard c++ library, but I am guessing your prof / book does not want you using them here.
EDIT: btw, "pToProduct" is a horribly confusing and misleading name. I'd call it "productPointersSortedByID" or something like that.
EDIT: also "IntArrayPtr" is bad too, sounds like the type is a pointer to an array of integers. I'd call it "CProductPtr." Or just skip doing the typedef altogether, simply " CProduct* " is clear enough.
Bookmarks