Last active
August 31, 2021 13:20
-
-
Save lightdiscord/adb459625dbf1e24665857bdc0a9feb3 to your computer and use it in GitHub Desktop.
Checking if vector re-allocation invoke the copy constructor of each element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
class Item { | |
private: | |
char identifier; | |
public: | |
Item(char identifier) : identifier(identifier) { | |
std::cout << "Item's " << identifier << " constructor called" << std::endl; | |
} | |
Item(const Item &src) : identifier(src.identifier) { | |
std::cout << "Item's " << identifier << " copy constructor called" << std::endl; | |
} | |
~Item(void) { | |
std::cout << "Item's " << identifier << " destructor called" << std::endl; | |
} | |
Item& operator=(const Item &src) { | |
identifier = src.identifier; | |
std::cout << "Item's " << src.identifier << " assignation called" << std::endl; | |
return *this; | |
} | |
}; | |
int main(void) { | |
std::vector<Item> items; | |
std::cout << "push_back" << std::endl; | |
items.push_back(Item('a')); | |
std::cout << "push_back" << std::endl; | |
items.push_back(Item('b')); | |
std::cout << "push_back" << std::endl; | |
items.push_back(Item('c')); | |
std::cout << "reserve" << std::endl; | |
items.reserve(50); | |
std::cout << "push_back" << std::endl; | |
items.push_back(Item('d')); | |
std::cout << "end" << std::endl; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ g++ -std=c++98 -o test main.cpp && ./test | |
push_back | |
Item's a constructor called | |
Item's a copy constructor called | |
Item's a destructor called | |
push_back | |
Item's b constructor called | |
Item's b copy constructor called | |
Item's a copy constructor called | |
Item's a destructor called | |
Item's b destructor called | |
push_back | |
Item's c constructor called | |
Item's c copy constructor called | |
Item's a copy constructor called | |
Item's b copy constructor called | |
Item's a destructor called | |
Item's b destructor called | |
Item's c destructor called | |
reserve | |
Item's a copy constructor called | |
Item's b copy constructor called | |
Item's c copy constructor called | |
Item's a destructor called | |
Item's b destructor called | |
Item's c destructor called | |
push_back | |
Item's d constructor called | |
Item's d copy constructor called | |
Item's d destructor called | |
end | |
Item's a destructor called | |
Item's b destructor called | |
Item's c destructor called | |
Item's d destructor called |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment