7.1 New()

The method New() creates a new element at the end of the container. At the same time, it returns a reference to this new element, which is why can use the set() method of circle in the example above.

But suppose you need to use two methods of the newly created object? You could try something like this:

 
for(int i = 0; i < 10; i++) 
{ 
  circles.New().set(0.1, RandomF(-D.w(), D.w()), RandomF(-D.h(), D.h())); 
  circles.New().extend(-0.05); 
5}  

…but it won’t work. Instead you are creating two new circles at every iteration. The method set is called on the first circle, the method extend at the second. The solution is simple: Pass the result of New() to a temporary variable. The type of this variable must be a reference to a circle.

 
for(int i = 0; i < 10; i++) 
{ 
  Circle & c = circles.New(); 
  c.set(0.1, RandomF(-D.w(), D.w()), RandomF(-D.h(), D.h())); 
5  c.extend(-0.05); 
}  

Note
If you don’t know what a reference is, don’t worry. We’ll talk about it later. For now, remember that you need to put an ampersand (&) between the type and the name.