Lemonade,
This is your code, turned into a complete little program:
------------------------------------------------------------------------
#using <iostream>
#using <string>
using namespace std;
class Item {
char *title;
public:
void set(char *ttl) { title = ttl; }
char *get() { return title; }
};
ostream &operator <<(ostream &os, Item item) {
return os << item.get() << endl;
}
class Inventory {
Item *itemList;
int idx, count, size;
public:
Inventory() { count = 0; size = 0; itemList = new Item[0]; }
void appendItem (char ttl[]);
void resizeArray();
int getCount() { return count; }
Item getItem(int index) { return itemList[index]; }
};
ostream &operator <<(ostream &os, Inventory &inventory) {
for (int i = 0; i < inventory.getCount(); i++) {
os << inventory.getItem(i);
}
return os;
}
void Inventory::appendItem (char ttl[])
{
if (count == size)
{
resizeArray();
}
itemList[count++].set(ttl);
}
void Inventory::resizeArray()
{
Item *itm = new Item[++size]; // instead of size++
if (itm == 0) {
cout << "Out of memory\n";
exit(1);
}
for (int i = 0; i < count; i++)
{
itm[i] = itemList[i];
}
delete [] itemList;
itemList = itm;
}
int _tmain(int argc, _TCHAR* argv[])
{
Inventory inventory;
inventory.appendItem("Hello");
inventory.appendItem("Bye");
cout << inventory;
string s;
cin >> s;
return 0;
}
------------------------------------------------------------------------
In the above code, Item objects have value semantics, in the sense that
their lifetime and their identity do not matter. In the above code, an
Item object is really just a container for a char *.
Now follows the version where the Item objects are allocated on the heap
and pointers to them are stored in itemList. Lifetime and identity are
important.
------------------------------------------------------------------------
#using <iostream>
#using <string>
using namespace std;
class Item {
char *title;
public:
Item(char *title) { this->title = title; }
char *get() { return title; }
};
ostream &operator <<(ostream &os, Item *item) {
return os << item->get() << endl;
}
class Inventory {
Item **itemList;
int idx, count, size;
public:
Inventory() { count = 0; size = 0; itemList = new Item*[0]; }
void appendItem (char ttl[]);
void resizeArray();
int getCount() { return count; }
Item *getItem(int index) { return itemList[index]; }
};
ostream &operator <<(ostream &os, Inventory &inventory) {
for (int i = 0; i < inventory.getCount(); i++) {
os << inventory.getItem(i);
}
return os;
}
void Inventory::appendItem (char ttl[])
{
if (count == size)
{
resizeArray();
}
itemList[count++] = new Item(ttl);
}
void Inventory::resizeArray()
{
Item **itm = new Item*[++size]; // instead of size++
if (itm == 0) {
cout << "Out of memory\n";
exit(1);
}
for (int i = 0; i < count; i++)
{
itm[i] = itemList[i];
}
delete [] itemList;
itemList = itm;
}
int _tmain(int argc, _TCHAR* argv[])
{
Inventory inventory;
inventory.appendItem("Hello");
inventory.appendItem("Bye");
cout << inventory;
string s;
cin >> s;
return 0;
}
------------------------------------------------------------------------
Bart Jacobs