[英]How to use *this from inside a class member function to swap its own address with the address of a passed pointer (of same type)
好的,這是一個名為dLinikedList的 class(是的,我知道我應該使用 STL 容器):
class dLinkedList
{
public:
//constructors will go here
explicit dLinkedList(const int value)
{
createFirstNode(value);
nodeCount = new size_t;
updateNodeCount();
}
dLinkedList(const dLinkedList &rhs)
{
Node *temp = rhs.head;
createFirstNode(temp->data);
nodeCount = new size_t;
updateNodeCount();
temp = temp->next;
while(temp)
{
push_back(temp->data);
temp = temp->next;
}
updateNodeCount();
}
explicit dLinkedList(size_t numberOfNode, int initializationValue)
{
createFirstNode(initializationValue);
for(size_t i = 1; i < numberOfNode; ++i)
push_back(initializationValue);
nodeCount = new size_t;
updateNodeCount();
}
//class destructor will go here
~dLinkedList()
{
clear();
delete nodeCount;
nodeCount = nullptr;
}
//member functions will go here
void push_back(int); // will attach a new node at the end of the list
void push_front(int); // will insert a new node at the beginning of the list
bool insertNode(int, int, bool, bool); // will insert a new node after the existing node (true = first occurrence from the head with value int OTHERWISE if false, then from the tail.)
bool deleteNode(int, bool); // will delete the existing node (true = first occurrence from the head with value int OTHERWISE if false, then from the tail.)
void pop_back(); // will delete the last node in the list and return the value of the internal data
void pop_front(); // will delete the first node in the list
size_t size(); // will return the number of nodes/elements - experimental feature
void printList(bool); // will print the values of the data - (true for ordered list, false for reverse ordered list)
void swap(dLinkedList &rhs); // will swap this linked-list with rhs
//operator overloading go here
dLinkedList& operator = (const dLinkedList &rhs);
dLinkedList& operator + (const dLinkedList &rhs);
dLinkedList& operator += (const dLinkedList &rhs);
dLinkedList& operator >> (const size_t numberOfNodes);
dLinkedList& operator << (const dLinkedList &rhs);
private:
//defining the double linked-list structure
struct Node
{
int data; //this is a generic place holder - will be replaced later with some actual data-structures
Node *next;
Node *previous;
explicit Node(int x) : data(x), next(nullptr), previous(nullptr) {}
};
//member functions go here
void createFirstNode(int val); //will create the first node when the list is empty
void clear(); // will be called when class destructor is called
void updateNodeCount(); // keeps the nodeCount variable up-to-date
bool empty(); // returns true if the list is empty
//some experimental utility functions for internal use
void ectomizeAndClip(Node*);
Node *detectData(int, bool);
void insertBefore(Node*, int);
void insertAfter(Node*, int);
//member variables go here
Node *head {nullptr};
Node *tail {nullptr};
size_t *nodeCount {nullptr}; //experimental feature
};
有這個成員function,目前實現為:
void dLinkedList::swap(dLinkedList &rhs)
{
dLinkedList temp {rhs};
rhs.clear();
Node *traverser = head;
while(traverser != nullptr)
{
rhs.push_back(traverser->data);
traverser = traverser->next;
}
clear();
traverser = temp.head;
while(traverser != nullptr)
{
push_back(traverser->data);
traverser = traverser->next;
}
}
顯然,隨着列表長度的增加,此操作會花費大量時間。 這是我的想法(如果可能的話 - 為了在較大列表的情況下最大限度地減少執行時間):
void dLinkedList::swap(dLinkedList *rhs)
{
// what I am planning to achieve is simply swap the address mutually.
dLinkedList *temp {rhs};
rhs = this;
this = temp;
}
但是此代碼不起作用,並給出如下錯誤:
error: lvalue required as left operand of assignment|
我想澄清我對這是否可以實現的疑慮/誤解? 如果是,那將是什么代碼。
this
是一個常量/不可變指針dLinkedList* const this
並且不能被this = temp
修改。
使用標准交換
void dLinkedList::swap(dLinkedList *rhs)
{
std::swap(*this, *rhs);
}
function 參數相當不正確,應該是
void dLinkedList::swap(dLinkedList &rhs)
{
std::swap(*this, rhs);
}
您的 class 由三個指針組成,只需交換這些指針即可。 這是非常有效的,並且不依賴於列表的大小。
像這樣
void dLinkedList::swap(dLinkedList &rhs)
{
std::swap(head, rhs.head);
std::swap(tail, rhs.tail);
std::swap(nodeCount, rhs.nodeCount);
}
有時,如果節點包含指向包含 object 的指針,則此方法將不起作用,但此處並非如此。
您可以在 swap function 中獲取要交換的dLinkedList
作為參考,然后使用std::swap
this
和rhs
的head
。
void dLinkedList::swap(dLinkedList& rhs) {
std::swap(this->head, rhs.head);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.