你怎麼稱呼爲具有私有成員模板類的拷貝構造函數,這也是另一種模板對象一個堆棧類的拷貝構造函數使用單級
我建立它使用列表類構建一個堆棧類堆疊。 列表類有一個拷貝構造函數, 所以我想複製棧1 = stack2中 我怎麼把它在棧中拷貝構造函數
的最後一個代碼的拷貝構造函數的棧類和我的拷貝構造函數我試圖複製私有成員list_3 myData
當我做myData = src.myData; //它複製相同的地址,不給新的對象
template <class ItemType>
class List_3
{
public:
typedef size_t size_type;
List_3();
List_3(const List_3 & src);
~List_3();
void insert(const ItemType & item);
void remove();
void reset();
bool advance();
bool isEmpty() const;
bool atEOL() const;
bool isFull() const;
ItemType getCurrent() const;
private:
struct Node {
ItemType value;
Node* next;
Node* previous;
};
Node* head;
Node* tail;
Node* cursor;
};
//Copy Constructor for List class**
template<class ItemType>
List_3<ItemType>::List_3(const List_3<ItemType> & src)
{
//Copy constructor
head = NULL;
tail = NULL;
cursor = NULL;
Node *tempCursor = new Node;
tempCursor = src.head; // copying the original list from head to tail
if (!src.isEmpty()) { //if the src list is not empty start copy process
while (tempCursor != NULL)
{
insert(tempCursor->value);
cursor = NULL;
tempCursor = tempCursor->next; //Move to the next item in the list use previous if copying from tail
}
reset(); //Reset the cursor
}
}
**//============================================================================**
template<class ItemType>
class Stack_3 {
public:
typedef int size_type;
Stack_3();
//Copy constructor
Stack_3(const Stack_3 & src);
void makeEmpty();
bool isEmpty() const;
bool isFull() const;
void push(const ItemType &);
ItemType pop();
private:
List_3<ItemType> myData;
};
**//Copy Constructor for Stack Class**
template<class ItemType>
Stack_3358<ItemType>::Stack_3358(const Stack_3358<ItemType> & src)
{
myData = src.myData;
}
閱讀您最喜愛的C++教科書中的「構造函數初始值設定項列表」。它是這樣的:'Stack_3358 :: Stack_3358(const Stack_3358 &src):myData(src.myData){...}'。話雖如此,因爲'List_3'需要一個用戶定義的拷貝構造函數,機會很高,它也需要一個用戶定義的拷貝賦值操作符。 –
Thx爲您的答案 – Rizzim