https://github.com/goblinhack/c-plus-plus-move-constructor-example
c++ move and copy constructor example with a custom vector class
https://github.com/goblinhack/c-plus-plus-move-constructor-example
Last synced: 12 days ago
JSON representation
c++ move and copy constructor example with a custom vector class
- Host: GitHub
- URL: https://github.com/goblinhack/c-plus-plus-move-constructor-example
- Owner: goblinhack
- Created: 2020-05-13T19:06:11.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-05-13T19:15:57.000Z (almost 6 years ago)
- Last Synced: 2025-06-11T18:26:13.495Z (8 months ago)
- Size: 1000 Bytes
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Example use of the move and copy constructors with a custom class
=================================================================
Expected output:
```
c: [10][11]
copy ctor called
copy of c: [10][11]
move ctor called
moved c: [10][11]
```
Compile as:
```
g++ -std=c++2a -O2 -Wall -pedantic foo.cpp
```
Code:
```C++
#include
#include
template class MyVector {
private:
T *data;
size_t maxlen;
size_t currlen;
public:
MyVector () : data (nullptr), maxlen(0), currlen(0) { }
MyVector (int maxlen) : data (new T [maxlen]), maxlen(maxlen), currlen(0) { }
MyVector (const MyVector& o) {
std::cout << "copy ctor called" << std::endl;
data = new T [o.maxlen];
maxlen = o.maxlen;
currlen = o.currlen;
std::copy(o.data, o.data + o.maxlen, data);
}
MyVector (const MyVector&& o) {
std::cout << "move ctor called" << std::endl;
data = o.data;
maxlen = o.maxlen;
currlen = o.currlen;
}
void push_back (const T& i) {
if (currlen >= maxlen) {
maxlen *= 2;
auto newdata = new T [maxlen];
std::copy(data, data + currlen, newdata);
if (data) {
delete[] data;
}
data = newdata;
}
data[currlen++] = i;
}
friend std::ostream& operator<<(std::ostream &os, const MyVector& o) {
auto s = o.data;
auto e = o.data + o.currlen;;
while (s < e) {
os << "[" << *s << "]";
s++;
}
return os;
}
};
int main() {
auto c = new MyVector(1);
c->push_back(10);
c->push_back(11);
std::cout << "c: " << *c << std::endl;
auto d = *c;
std::cout << "copy of c: " << d << std::endl;
auto e = std::move(*c);
delete c;
std::cout << "moved c: " << e << std::endl;
}
```