← Back to Pointers
Question 714
TokenBuffer Move Constructor Leaves the Source Empty
TokenBuffer Move Constructor Leaves the Source Empty
Write a class TokenBuffer that owns an int array on the heap and supports both COPYING (a deep copy) and MOVING (steal the pointer and leave the source owning nothing). Do NOT write a main function; a hidden test provides one. Required members: - TokenBuffer(int n) - allocates n ints withnew[], every element 0, and remembers the count. - TokenBuffer(const TokenBuffer& other) - a deep copy: its own fresh array holding the same values. - TokenBuffer(TokenBuffer&& other) - the MOVE constructor: takes over other's pointer, then sets other's pointer to nullptr and other's count to 0. - TokenBuffer& operator=(TokenBuffer&& other) - move assignment: release whatever this object owns, take over other's pointer, null out other, and return *this. - ~TokenBuffer() - releases the array. - intsize()const, boolempty()const (true when the buffer owns no elements), voidset(int index, int value), and intget(int index)const. Free the memory you allocate - this question checks for memory leaks. The hidden test reads n numbers into a buffer and prints its size, first value and whether it is empty. It copy-constructs a second buffer, sets the copy's first value to -1 and prints both first values. It move-constructs a third buffer from the first and prints the third's size and first value, then the moved-from buffer's size and empty state. Finally it move-assigns the copy into a one-element buffer and prints that buffer's size and first value, then the moved-from copy's size and empty state. For the input: 3 4 5 6 your class must produce exactly the output shown in Expected Output.
Expected Output:
3 4 full 4 -1 3 4 0 empty 3 -1 0 empty
= newline. The output ends with a newline — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Pointers
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.