In this post, you’ll get answers to these questions:
- Can I use linked lists with unique_ptr in a modern way?
- How can I use linked list with the class, struct combinations?
- How can I use smart pointers with linked lists?
By learning how to use Powerful Modern Linked Lists in c++, it will help you to build C++ applications with the use of C++ Software.
Table of Contents
What does the smart pointer unique_ptr mean?
std::unique_ptr is a smart pointer (Since C++11) that manages and owns another object with a pointer. When the unique_ptr goes out of scope the runtime disposes of that object automatically.
The object at that pointer address is disposed of by its associated “deleter” . This happens If the unique_ptr object is destroyed or if the unique_ptr object is assigned another pointer by the operator ‘=’ or by the reset() method. Thus the user do not need to free or delete the smart pointer. The default deleter uses the delete operator which destroys the object and deallocates the memory. A unique_ptr may have no object that means it is empty.
std::unique_ptr
can be used with the dynamically-allocated array of objects and with the single objects.
What is the syntax for unique_ptr?
1 2 3 |
template < class T, class Deleter = std::default_delete <T> > class unique_ptr; |
1 2 3 |
template < class T, class Deleter > class unique_ptr < T[], Deleter >; |
Here is a unique_ptr example used with a struct,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <memory> struct st_dat { int X, Y; }; int main() { std::unique_ptr<st_dat> dat( new st_dat ); dat->X = 30; dat->Y = 20; std::cout << dat->X << "," << dat->Y << std::endl; getchar(); return 0; } |
How to use modern linked lists with std::unique_ptr?
First let’s create a Type class (TLinkedList) for our modern linked list,
1 2 3 4 5 6 |
class TForm1::TLinkedList { public: }; |
under this public: definition we can define our st_node struct.
1 2 3 4 5 6 7 |
struct st_node { int val; std::unique_ptr<st_node> next; }; |
and let’s keep adding more lines. We can define the head of this st_node by using std::unique_ptr as below,
1 2 3 |
std::unique_ptr<st_node> head; |
and in this class we can define a add_value() method to add our values as give below,
1 2 3 4 5 6 |
void add_value(int val) // Add Value { head = std::unique_ptr<st_node>(new st_node{val, std::move(head)}); } |
finally we can add destructor ~TLinkedList() method to delete all linked list from the memory
1 2 3 4 5 6 |
~TLinkedList() // Linked List Destructor { while(head) head = std::move(head->next); } |
Remember that destructor name should be same as your linked list class. As a result, our class will be as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class TForm1::TLinkedList { public: struct st_node { int val; std::unique_ptr<st_node> next; }; std::unique_ptr<st_node> head; void add_value(int val) // Add Value { head = std::unique_ptr<st_node>(new st_node{val, std::move(head)}); } ~TLinkedList() // Linked List Destructor { while(head) head = std::move(head->next); } }; |
How do I declare an instance of TLinkedList class?
Now we should declare TLinkedList class in our VCL form,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//--------------------------------------------------------------------------- #ifndef Modern_Linked_List_Unit1H #define Modern_Linked_List_Unit1H //--------------------------------------------------------------------------- #include <System.Classes.hpp> #include <Vcl.Controls.hpp> #include <Vcl.StdCtrls.hpp> #include <Vcl.Forms.hpp> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); class TLinkedList; }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif |
Thus we have a modern linked list that destroy it self safely. We can create a linked list (linked_list) by using our linked list type (TLinkedList) in any procedure. For example,
1 2 3 4 5 6 7 8 9 10 |
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { TLinkedList linked_list; linked_list.add_value(76); linked_list.add_value(15); linked_list.add_value(47); } |
This TLinkedList class can be used in the most of C++ compilers (C++11, C++14, C++17). Here is the full C++ Builder VCL file,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
//--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "Modern_Linked_List_Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- class TForm1::TLinkedList { public: struct st_node { int val; std::unique_ptr<st_node> next; }; std::unique_ptr<st_node> head; void add_value(int val) // Add Value { head = std::unique_ptr<st_node>(new st_node{val, std::move(head)}); } ~TLinkedList() // Linked List Destructor { while(head) head = std::move(head->next); } }; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { TLinkedList linked_list; linked_list.add_value(76); linked_list.add_value(15); linked_list.add_value(47); } //--------------------------------------------------------------------------- |
How do I declare the TLinkedList Class in my C++ app?
Don’t forget to add class declaration in header file of the form in public as below,
1 |
class TLinkedList; |
This is a very modern and unique example that combines the class and the struct together with a linked list created by std::unique_ptr.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition