簡體   English   中英

對象的動態數組

[英]Dynamic array of objects

我知道我可以使用一種叫做std::vector的東西,但由於課程限制,我擔心這是不可能的。

我需要制作一個動態可擴展的對象數組。 當需要存儲新對象時,數組應該不斷增長。

這里是數組所屬的class:

class TransactionList
{
    private:
        Transaction *trans;
        int amountTransactions;
        Transaction newTrans;

    public:
        TransactionList();
        ~TransactionList();
        void read( istream &is );
        void write( ostream &os );
        void add( Transaction & newTrans ); 
        double totalExpenses();
        double hasPaid( string namnet );
        double isDebted( string namnet );
        //PersonList FixPersons();
 };

“void add (Transaction & newTrans)”方法是我需要的。 是的,我真的必須做指針式的。

到目前為止,這種方法是完全不完整的,甚至還沒有接近功能。 我嘗試了幾種不同的方法,但最終會出現運行時錯誤或只是胡說八道。

void TransactionList::add(Transaction & newTrans)
{
    Transaction* tempArrPtr;

    amountTransactions++;
    trans = new Transaction[amountTransactions]
    trans[amountTransactions - 1] = newTrans;
}

我想要的方法是構建一個事務對象數組,並在獲得更多對象時增大大小。

我希望我已經清楚地寫了我的問題,並希望有人能給我一個好的答案。 我試過谷歌搜索,但我仍然卡住 - 否則我不會打擾問:p

另外,如果有人可以提供一些有關復制構造函數的指示,我將非常感激。 在我們的課程材料中,它們幾乎沒有顯示復制構造函數應該是什么樣子。 就像“你需要復制構造函數來進行深度復制,祝你好運!”

您應該添加一個maxTransactions變量,該變量將指示您的 trans* 數組的分配長度,並將ammountTransactionsmaxTransactions都初始化為 0。

當我們達到 trans 的限制時,您的數組會自動將其大小加倍


void TransactionList::add(Transaction & newTrans)
{
    if(amountTransactions == maxTransactions){ //we've reached the capacity of trans
        //allocate a new array
        Transaction* nuTrans = new Transaction[maxTransactions*2+1];
        //copy the old values of trans into nuTrans
        memcpy(nuTrans, trans, sizeof(Transaction)*maxTransactions);
        //deallocate the old trans array
        delete []trans;
        //set trans to point at your newly allocated array
        trans = nuTrans;
        //update maxTransactions
        maxTransactions = maxTransactions*2+1;
    }

    trans[amountTransactions] = newTrans;
    amountTransactions++;
}


PS。 我這里是直接寫的,是整體編譯還是沒有調試代碼我沒檢查。 但我把它作為一個你可以遵循的想法

編輯:工作示例@ http://ideone.com/uz1mE

當您添加 object 並且陣列太小時,您需要創建一個具有正確或更大尺寸的新陣列,復制數據,刪除舊陣列並用新陣列替換。

復制構造函數與普通構造函數一樣,只是它們采用相同類型的 object。 請記住在執行此操作時要妥善保管好您的指針。

 TransactionList(const TransactionList & o);

現在我終於設法解決了這個難題。 事實證明,我沒有足夠注意我的事務對象本身持有一個動態數組這一事實,所以我最終想到了創建分配 function 來復制對象的想法。 以為我會分享我的解決方案,以防萬一有人必須使用相同的有限工具集來解決相同的問題。

這就是它最終的樣子:

   void TransactionList::add(Transaction & newTrans)

   {
    amountTransactions++;

    cout << "Adding a transaction-object to the array. amountTransactions = " << amountTransactions << endl;
    //Allocate a new array
    Transaction* tempTrans = new Transaction[amountTransactions];
    //Copy the objects with the assign-function
    for (int i = 0; i < amountTransactions - 1; i++)
            tempTrans[i].assign(trans[i]);

    //Delete the old one
    delete[] trans;

    //Set trans to point at the new one
    trans = tempTrans;

    //Add the newcomer object
    trans[amountTransactions - 1].assign(newTrans);
}

分配函數如下所示:

void Transaction::assign(const Transaction & t)
{
    date = t.date;
    type = t.type;
    name = t.name;
    amount = t.amount;
    amountFriends = t.amountFriends;

    cout << "Hello assign " << amountFriends << endl;

    delete [] friends;
    if (amountFriends > 0)
    {
        friends = new string[amountFriends];

        for (int i = 0; i < amountFriends; i++)
            friends[i] = t.friends[i];
    }

    else
        friends = NULL;
}

我的最終解決方案是基於 matyas 的回答,所以我欠你一個朋友:。)還要感謝 Alexandre C. 的閱讀!

我沒有排除代碼中可能存在一些錯誤的可能性,但至少它可以編譯、運行並產生正確的結果。 如果您發現不正確的地方,請隨時指出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM