1
我只是用STL開始,說我有一個兔類,現在我創造一個兔子軍團...將堆棧中最初的對象推到一個向量中,對象是否會丟失?
#include <vector>
vector<rabbit> rabbitArmy (numOfRabbits,rabbit());
//Q1: these rabbits are on the heap right?
rabbit* rabbitOnHeap = new rabbit();
//Q2: rabbitOnHeap is on the heap right?
rabbit rabbitOnStack;
//Q3: this rabbit is on the stack right?
rabbitArmy.push_back(rabbitOnStack);
//Q4: rabbitOnStack will remain stored on the stack?
//And it will be deleted automatically, though it's put in the rabbitArmy now?
Q4是一個我最關心的是,我應該總是使用新的關鍵字添加兔子到我的軍隊?
Q5:有沒有更好的方式來兔子添加到軍隊比:
rabbitArmy.push_back(*rabbitOnHeap);
超詳細!!!萬分感謝! – Arch1tect 2013-02-13 05:20:58