Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- how a vector of objects (with static members) is constructed
- 1 2 3 4 5
- 6 7 8 9 10
- 1 2 3 4 5
- 6 6 6 6 6
- // ==== test.h =====
- using namespace std;
- void test();
- class Record{
- static int total_number;
- int id;
- public:
- Record();
- void show() {std::cout << id << " "; }
- };
- // ==== test.cpp ====
- #include "stdafx.h"
- #include <iostream>
- #include <vector>
- #include "test.h"
- using namespace std;
- Record::Record(){
- total_number += 1;
- id = total_number;
- }
- void test(){
- const int vec_length = 5;
- Record a[vec_length];
- for (unsigned int i=0; i<vec_length; i++)
- a[i].show();
- cout << endl;
- vector<Record> vr(vec_length);
- for (unsigned int i=0; i<vr.size(); i++)
- vr[i].show();
- cout << endl;
- }
- // ==== main.cpp =====
- #include "stdafx.h"
- #include <iostream>
- #include "test.h"
- using namespace std;
- int Record::total_number = 0;
- int _tmain(int argc, _TCHAR* argv[])
- {
- test();
- return 0;
- }
- std::vector<Record> v(5, Record());
- std::vector<Record> v(5);
- Record::Record() : id(++total_number) {}
- Record::Record(const Record &other) : id(other.id) {}
Add Comment
Please, Sign In to add comment