Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / December 2003

Tip: Looking for answers? Try searching our database.

dynamic array of pointers vs dynamic array of objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
lemonade - 10 Dec 2003 18:18 GMT
Hello!
Can someone explain to me the difference between dynamic
array of pointers vs dynamic array of objects by giving a
real life example. Following is the code that I am using
for dynamic array of objects. I am skipping initialization
and other member functions.

class Inventory {
Item *itemList;
int idx, count, size;
public:
void appendItem (char ttl[]);
void resizeArray();
};

void Inventory::appendItem (char ttl[])
{ if (count == size)
{resizeArray(); }
itemList[count].set(ttl)

}

void Inventory::resizeArray()
{Item *itm = new Item[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;
}

how this code will change to allocate an array of item
pointers of type Item*?????
Bart Jacobs - 10 Dec 2003 20:31 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.