Move semantics in C++11
阿新 • • 發佈:2018-12-23
After studying an example for Move Semantics of C++11, I write a more complete code snippet:
C++1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 | #include <iostream>usingnamespacestd;classIntvec{public:explicitIntvec(size_t num=0):m_size(num),m_data(newint[m_size |
Pay attention to last two lines in ‘move constructor’:
C++1234 | ...other.m_size=0;other.m_data=nullptr;... |
Since ‘move constructor’ will not set initial value for m_size and m_data of ‘v4’, the m_size and m_data of ‘v3’ will be uninitial after swaps. Adding the two lines of code means to initialize m_size and m_data of ‘v3’.