This C++ Pointer
shows you how to manipulate pointers stored in an STL Vector and an STL Map. There are other STL containers, but once the use of these two containers is known the same techniques can be used for the others.
Why store a pointer in an STL container?
To make the example easily understandable and focus on the code required for using the STL containers, rather than the application, the example code uses simple classes to handle HTML colour codes. The use cases are very simple and a bit contrived, but the aim is to show how to use a pointer stored in a container.
The demonstration program uses a base class that stores a 24-bit colour code and the colour name, ColourCode. The name of the colour and the colour value can't be changed in the base class. ColourCode objects are created from HTML colour code data for a small number of colours in Loader.cpp
A Vector is used to store unique_ptr
s of colour code objects, not of pointers to the objects.
A number of objects of a class derived from ColourCode, that allows the colour code name and value to be changed, are created and added to the Vector of ColourCode objects created from the data file
.
After the colour code data has been loaded a Vector is used to store a pointer for each ColourCode object, used for example to store the load order of the data. A redundant exercise, but done to demonstrate using a Vector of pointers.
A Map is created and used to store a pointer for each ColourCode object with a key, the colour code's name, to provide a fast look up of a colour code.
The contents of the Vector and Map are written to results.txt to demonstrate the contents of both the same.
The customisable ColourCode objects are changed to hold the 7 additional colours, (ignoring the second occurrence of black), of the 4-bit RGBI palette. The Map of pointers of the ColourCode objects is used to find the customisable ColourCode objects by name.
Because we can contain pointers to derived classes in a Container of pointers of the base class, a dynamic cast
enables us to select a specific class that implements specific functionality, without having to fill the base class with all the virtual functions that are implemented somewhere in the inheritance tree.
If the dynamic_cast
is successful, i.e. not NULL
we can easily do class specific actions on the object.
The contents of the Vector and Map are written to results.txt to demonstrate the contents of both contain the same updated data.
The Map and Vector of ColourCode pointers are cleared. When the Vector of ColourCodes is cleared, the destruction of the contained unique_ptr
s is displayed.
This C++ Pointer
assumes you know how to use a Vector or Map to hold primitives, as this is widely described in literature. If you want to know how to use the STL Vector or Map check out www.cppreference.com/cppstl.html or Google etc., for information on the Standard Template Library.
C++ Pointeris distributed with
A Vector or Map of Pointers is created in the same way that a Vector or Map of primitives, objects or struct
s are created, (see my C++ Pointer
Objects, structs and STL Containers) as shown following, (see TSSCppPtr4.cpp for actual code).
A Pointer is added to an STL Container the same why that primitives, objects or struct
s are added, (see my C++ Pointer
Objects, structs and STL Containers) as shown following, (see TSSCppPtr4.cpp for actual code).
Note in TSSCppPtr4.cpp, the Vector loadOrder is loaded with the pointer contained in the std::unique_ptr
, so the pointer is obtained with .get()
of the unique_ptr
. In this example it's more convenient
to load the Map colourCodeIndex with the pointer stored in Vector loaderOrder rather then extract the pointer from the Vector of ColourCode objects.
An Iterator for an STL Container of Pointers is created in the same way that an iterator would be created for a Container of primitives, objects or struct
s are created, (see my C++ Pointer
Objects, structs and STL Containers). The addition of type deduction, (auto
) to C++ simplifies iterator creation and in this example only auto
constructed iterators are used.
struct
Pointed to by the Pointer Stored in an STL ContainerThe member functions of an object or struct
pointed to by the Pointer in a Container are accessed the same as if the Container stored objects or struct
s, (see my C++ Pointer
Objects, structs and STL Containers).
Using an iterator created for the Container, which is used as a pointer to the object, use the -> (structure pointer dereference) operator in place of the dot operator to access the object's or struct
's member function. For example;
A pointer in a Container can be passed into a function that takes a reference to the object, by dereferencing the iterator for the Container. For example
Note, in the function the pointer referenced by the iterator is now treated as if it is an object so the member functions are referenced using the . (dot) operator.
A pointer in a Map can also be passed into a function that takes the reference of a Map Iterator, which you can do if you also want the value of the Map Key in the function, for example
Note, because we are passing an iterator into the iterator reference parameter of a function, the iterator is not dereferenced in the function call, and within the function the variable is an iterator and the first and second parameters of the iterator are referenced with the -> (arrow) (pointer dereference) operator.
This C++ Pointer
was compiled and tested using Apple clang version 14.0.0 on x86_64-apple-darwin22.3.0 and with Windows 10 using cygwin-3.2.0, gcc version 10.2.0
To obtain the source code, install and execute this C++ Pointer
use the following procedure.
TSSCppPtr4
and place the source files in itTSSCppPtr4
folderTSSCppPtr4.cpp
, which contains the code to demonstrate using Pointers in a Vector and in a MapIf you suspect a bug please e-mail a description being sure to identify that the bug report is about C++ Pointer #4 and include as much detail as possible.