1#include <iostream>
2#include <vector>
3
4class Object
5{
6 public:
7 Object()
8 {}
9 ~Object()
10 {}
11 void AddInt(int num)
12 {
13 m_VectorOfInts.push_back(num);
14 }
15 std::vector<int> GetCopyOfVector()
16 {
17 return m_VectorOfInts;
18 }
19 void DisplayVectorContents()
20 {
21 for( unsigned int i = 0; i < m_VectorOfInts.size(); i++ )
22 {
23 std::cout << "Element[" << i << "] = " << m_VectorOfInts[i] << std::endl;
24 }
25 std::cout << std::endl;
26 }
27
28 private:
29 std::vector<int> m_VectorOfInts;
30};
31
32int main()
33{
34 // Create our class an add a few ints
35 Object obj;
36 obj.AddInt(32);
37 obj.AddInt(56);
38 obj.AddInt(21);
39
40 // Display the vector contents so far
41 obj.DisplayVectorContents();
42
43 // Creates a copy of the classes container you can only really view whats in
44 // the classes vector container. What ever changes you make here wont effect the class.
45 std::vector<int> container1 = obj.GetCopyOfVector();
46 // These elements wont be added as it's a copy of the container
47 container1.push_back(342);
48 container1.push_back(64);
49 container1.push_back(123);
50
51
52 // Display the classes container to see show nothing was added.
53 obj.DisplayVectorContents();
54
55 return 0;
56}