Skip to content

Commit af743b3

Browse files
committed
use std::move in move constructor and assignment operator
1 parent f1f8035 commit af743b3

File tree

6 files changed

+18
-19
lines changed

6 files changed

+18
-19
lines changed

ArrayList.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ ArrayList::ArrayList(const ArrayList &arrayToCopy)
2828
ArrayList::ArrayList(ArrayList&& arrayToMove)
2929
{
3030
std::cout << "Arraylist move constructor is called." << std::endl;
31-
this->capacity = arrayToMove.capacity;
32-
this->used = arrayToMove.used;
33-
this->pArray = arrayToMove.pArray;
31+
this->capacity = arrayToMove.getCapacity();
32+
this->used = arrayToMove.size();
33+
this->pArray = arrayToMove.getPArray();
3434
arrayToMove.capacity = 0;
3535
arrayToMove.used = 0;
3636
arrayToMove.pArray = nullptr;
@@ -63,9 +63,9 @@ ArrayList& ArrayList::operator=(ArrayList &&rhs)
6363
if (&rhs != this)
6464
{
6565
delete[] this->pArray;
66-
this->capacity = rhs.capacity;
67-
this->used = rhs.used;
68-
this->pArray = rhs.pArray;
66+
this->capacity = rhs.getCapacity();
67+
this->used = rhs.size();
68+
this->pArray = rhs.getPArray();
6969
rhs.capacity = 0;
7070
rhs.used = 0;
7171
rhs.pArray = nullptr;

ArrayListDriver

0 Bytes
Binary file not shown.

Token.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,11 @@ Token::Token(const Token& token)
4242
Token::Token(Token&& token)
4343
{
4444
std::cout << "Token move constructor is called." << std::endl;
45-
this->cstr = token.cstr;
46-
this->frequency = token.frequency;
47-
this->number_list = token.number_list;
45+
this->cstr = token.c_str();
46+
this->number_list = std::move(token.number_list);
47+
this->frequency = token.getFrequency();
4848
token.cstr = nullptr;
4949
token.frequency = 0;
50-
// token.number_list = nullptr; --> why is this not working?
5150
}
5251

5352
// copy assignment operator
@@ -77,9 +76,9 @@ Token& Token::operator=(Token &&rhs)
7776
if (&rhs != this)
7877
{
7978
delete[] this->cstr;
80-
this->frequency = rhs.frequency;
81-
this->number_list = rhs.number_list;
82-
this->cstr = rhs.cstr;
79+
this->frequency = rhs.getFrequency();
80+
this->number_list = std::move(rhs.number_list);
81+
this->cstr = rhs.c_str();
8382
rhs.cstr = nullptr;
8483
rhs.frequency = 0;
8584
}

Token.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ class Token
1414

1515
public:
1616
// constructors
17-
Token(); // default constructor
18-
Token(const char* chars, int line_num); // normal constructor
19-
Token(const Token& token); // copy constructor
20-
Token(Token&& token); // move constructor
17+
Token(); // default constructor
18+
explicit Token(const char* chars, int line_num); // normal constructor
19+
Token(const Token& token); // copy constructor
20+
Token(Token&& token); // move constructor
2121

2222
// assignment operators
2323
Token& operator=(const Token& rhs); // copy assignment operator

TokenDriver

120 Bytes
Binary file not shown.

TokenList.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class TokenList {
2626
private:
2727
TNode* head{ nullptr }; // points to the first node in the list
2828
TNode* tail{ nullptr }; // points to the last node in the list
29-
size_t size{ 0 }; // counts the number of noded in the list
29+
size_t size{ 0 }; // counts the number of nodes in the list
3030

3131
bool remove(TNode* nodePtr);
3232
TNode* lookup(const Token& aToken) const;
@@ -42,7 +42,7 @@ class TokenList {
4242
TokenList& operator=(const TokenList& rhs); // copy assignment operator
4343
TokenList& operator=(TokenList&& rhs); // move assignment operator
4444

45-
~TokenList();
45+
~TokenList(); // destructor
4646

4747
// member functions
4848
bool empty() const;

0 commit comments

Comments
 (0)