Creating a vector that holds objects

I am trying to create a program that can hold as many objects as the user wants. The objects will hold how many times it member method, 'data' has been called, and also will have a random age assigned to it. If all goes well the method 'printData' should print the values to the screen that are determined in 'data'.

I wasn't sure if I should use a vector of object pointers or not so I have commented out the code with pointers. The difference seems to be when I have
vector<Zoo*> ZooPtrVex; constructors will be called but no copy constructors and when I have vector<Zoo> zooVex; I call way too many copy constructors!

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
#include<vector>
#include "Zoo.h"
using namespace std;

int main()
{
	vector<Zoo> zooVex;//calls copy constructors
	Zoo zoo;
	//vector<Zoo*> ZooPtrVex; //using ptrs calls constructors
	//Zoo* zooPtr =new Zoo;

	cout << endl;
	
	for (int i = 0; i < 5; i++)
	{
		/*ZooPtrVex.push_back(new Zoo);
		zooPtr->data();*/
		
		zoo.data();
		zooVex.push_back(zoo);

	}
	

	for (int i = 0; i < 5; i++)
	{
		//ZooPtrVex[i]->printData();
		zooVex[i].printData();
	}


	//delete[] zooPtr;

	system("Pause");
	return 0;
}  

zoo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#pragma once
#include<iostream>
#include<vector>

using namespace std;

class Zoo
{
private:
	int Constructcount;
	int copyConstructCalls;
	int age;
	int dataCallCount;
public:
	Zoo():Constructcount(0),age(0), dataCallCount(0)
	{
		Constructcount++;
		cout << " Construct Calls" << Constructcount << endl;
	}

	//NumberArray(const NumberArray &obj)

	Zoo(const Zoo &z): copyConstructCalls(0)
	{
		copyConstructCalls++;
		cout << "copy Construct Calls" << copyConstructCalls << endl;
	}

	void data()
	{
		dataCallCount++;
		
		age = rand() % 100 + 1;
		
		
	}

	void printData()
	{
		cout << "data Call Count" << dataCallCount << endl;
		cout << age << endl;
	}

	


	~Zoo(){}


};

  


Either way I can't get 'printData' to display the the call count or age that are defined in 'data'
After some tinkering I have this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream>
#include<vector>
#include "Zoo.h"
using namespace std;

int main()
{
	int index = 1;
        vector<Zoo*> ZooPtrVex; 
	Zoo* zooPtr =new Zoo[index];

       //I want 5 objects so I will use for loop

         for (int i = 0; i < 5; i++)
	{

	ZooPtrVex.push_back(zooPtr->pData(zooPtr));
	index++;
	zooPtr = new Zoo[index+1];

	}

for (int i = 0; i < 5; i++)
	{
		ZooPtrVex[i]->printData();
}

	delete[] zooPtr;

	system("Pause");
	return 0;
}



How to I minimize the constructor count?! I've managed to call like 25 of them even though I only want 5 objects! LOL

My output...

Construct Calls1

data Call Count1
42
Construct Calls1
Construct Calls1
Construct Calls1
data Call Count1
68
Construct Calls1
Construct Calls1
Construct Calls1
Construct Calls1
data Call Count1
35
Construct Calls1
Construct Calls1
Construct Calls1
Construct Calls1
Construct Calls1
data Call Count1
1
Construct Calls1
Construct Calls1
Construct Calls1
Construct Calls1
Construct Calls1
Construct Calls1
data Call Count1
70
Construct Calls1
Construct Calls1
Construct Calls1
Construct Calls1
Construct Calls1
Construct Calls1
Construct Calls1

data Call Count1
42
data Call Count1
68
data Call Count1
35
data Call Count1
1
data Call Count1
70
Last edited on
Better instrumentation would help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include<iostream>
#include<vector>
#include<cstdlib>

using namespace std;

class Zoo
{
private:
	static int Constructcount;
	static int copyConstructCalls;
	static int dataCallCount;
	int age;
public:
	Zoo():age(0)
	{
		Constructcount++;
		cout << " Construct Calls " << Constructcount << endl;
	}

	//NumberArray(const NumberArray &obj)

	Zoo(const Zoo &z)
	{
		copyConstructCalls++;
		cout << "copy Construct Calls " << copyConstructCalls << endl;
	}

	void data()
	{
		dataCallCount++;
		age = rand() % 100 + 1;
	}

	void printData()
	{
		cout << "data Call Count " << dataCallCount << endl;
		cout << age << endl;
	}

	static void stats() {
		cout << "Ctor=" << Constructcount << endl;
		cout << "CCtor=" << copyConstructCalls << endl;
		cout << "Data=" << dataCallCount << endl;
	}

	~Zoo(){}
};
int Zoo::Constructcount = 0;
int Zoo::copyConstructCalls = 0;
int Zoo::dataCallCount = 0;

int main()
{
	vector<Zoo> zooVex;
	Zoo zoo;

	cout << "Begin loop" << endl;

	for (int i = 0; i < 5; i++)
	{
    cout << "Adding " << i << " to the vector" << endl;
		zoo.data();
		zooVex.push_back(zoo);
	}


	for (int i = 0; i < 5; i++)
	{
		zooVex[i].printData();
	}


	cout << "Summary" << endl;
	Zoo::stats();

	return 0;
}

$ ./a.out 
 Construct Calls 1
Begin loop
Adding 0 to the vector
copy Construct Calls 1
Adding 1 to the vector
copy Construct Calls 2
copy Construct Calls 3
Adding 2 to the vector
copy Construct Calls 4
copy Construct Calls 5
copy Construct Calls 6
Adding 3 to the vector
copy Construct Calls 7
Adding 4 to the vector
copy Construct Calls 8
copy Construct Calls 9
copy Construct Calls 10
copy Construct Calls 11
copy Construct Calls 12
data Call Count 5
0
data Call Count 5
0
data Call Count 5
0
data Call Count 5
0
data Call Count 5
0
Summary
Ctor=1
CCtor=12
Data=5


A lot of the excess copy ctors come about when the vector gets resized internally - where it allocates a new storage area and copies across all the old entries to the new larger space.

You could reduce this by doing say
zooVex.reserve(10);
Topic archived. No new replies allowed.