December 2018
Intermediate to advanced
702 pages
20h 9m
English
You can create objects on the stack or in the free store. Using the previous example, this is as follows:
cartesian_vector vec { 10, 10 }; cartesian_vector *pvec = new cartesian_vector { 5, 5 }; // use pvec delete pvec
This is direct initialization of the object and assumes that the data members of cartesian_vector are public. The vec object is created on the stack and initialized with an initializer list. In the second line, an object is created in the free store and initialized with an initializer list. The object on the free store must be freed at some point and this is carried out by deleting the pointer. The new operator will allocate enough memory in the free store for the data members of the class and for any of ...
Read now
Unlock full access