Skip to main content

Section 17.3 Objects in Vectors

There are not too many differences between using a vector of objects and a vector of structs. But there are a few interactions between vectors and the constructors and const members of classes worth noting.
On this page, we will make use of this SimplePoint1 AND SimplePoint2 classes. They are identical except for the fact that SimplePoint2 has a no-argument constructor:
export module SimplePoint;

export class SimplePoint1 {
public:
    SimplePoint1(double x, double y) {
        m_x = x;
        m_y = y;
    }

    double getX() const {
        return m_x;
    }
private:
    double m_x, m_y;
};


export class SimplePoint2 {
public:
    SimplePoint2() {
        m_x = 0;
        m_y = 0;
    }
    SimplePoint2(double x, double y) {
        m_x = x;
        m_y = y;
    }

    double getX() const {
        return m_x;
    }
private:
    double m_x, m_y;
};
First let’s try to create a vector of 5 SimplePoint1s:
Listing 17.3.1.
We immediate get an error:
error: no matching function for call to 'SimplePoint1::SimplePoint1()
When the code tries to set up the memory for SimplePoint1, it tries to initialize each object in the vestor. There is no instruction how to do so, so it is assumed we want to use the no-arg constructor for each of the Points. But SimplePoint1 does not have a no-arg constructor. To solve the problem, we have to either:
  • Provide a no-argument constructor for SimplePoint1. (We may not be able to do this if we are using someone else’s class.
  • Specify how to construct the Points in the vector.
  • Provide an explicit list of Points.
. This sample demonstrates the approaches:
Listing 17.3.2.
As shown on lines 23 and 24, once we have created the vector, we can access individual elements using the .at() function. Doing so names a particular Point that then we can call member functions on. So points1.at(0).getX() starts with the vector named points1, then gets the first element, which is a point, and then calls getX() on that point.
We can also loop through the elements by using a range based for loop. The loop on line 27 uses const SimplePoint1& as the type of each element. This way, we are using the actual objects from the vector and not making copies of them. We use const to promise that the vector elements will not change. But for that to work, we have to make sure that the getX() function is marked const. (Remember that const tends to be infectious!).

Checkpoint 17.3.1.

Checkpoint 17.3.2.

Which best describes what happens if we write vector<Circle> circleList(10);?
  • The code will attempt to call Circle(10) to initialize the memory of each Circle.
  • The code will attempt to call Circle() to initialize the memory of each Circle.
  • It will be a compiler error.
  • The code will create space for 10 Circles, but leave the memory uninitialized. (There will be random data.)
  • When we create a vector with a given size, the memory inside it is always initialized.
You have attempted of activities on this page.