Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* onetoomany.cpp constructs 11 items instead of 10 */
- #include <iostream>
- #include <vector>
- struct Item
- {
- public:
- static size_t ctorCount;
- static size_t dtorCount;
- static size_t cpctorCount;
- Item() { ctorCount++; }
- ~Item() { dtorCount++; }
- Item(const Item& item){ cpctorCount++; }
- };
- // must initialize static memebers out of line
- size_t Item::ctorCount = 0;
- size_t Item::dtorCount = 0;
- size_t Item::cpctorCount = 0;
- //creates count items in vector
- void populate( std::vector<Item> & vec, int count)
- {
- vec.reserve(count);
- vec.assign(count, Item());
- }
- int main()
- {
- size_t count = 10;
- std::vector<Item> vec;
- populate( vec, count);
- std::cout << "Size of vec:\t"
- << vec.size() << std::endl;
- std::cout<<"Total Item Objects constructed = "
- << (Item::ctorCount + Item::cpctorCount)
- << "\n";
- std::cout<<"Constructor called "
- << Item::ctorCount << " times"
- << "\n";
- std::cout<<"Copy Constructor called "
- << Item::cpctorCount <<" times"
- << "\n";
- std::cout<<"Total Item Objects destructed = "
- <<Item::dtorCount
- <<std::endl;
- }
- /** OUTPUT
- Size of vec: 10
- Total Item Objects constructed = 11
- Constructor called 1 times
- Copy Constructor called 10 times
- Total Item Objects destructed = 1
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement