Skip to content

Example on using std::unique_ptr from c++17 and how copy-constructors work

License

Notifications You must be signed in to change notification settings

conradhaupt/unique_ptr_example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

unique_ptr example

This code shows off how std::unique_ptr can be used with copies etc. See below for how to compile.

Requirements

  • gcc
  • cmake
  • make

Build process

Inside the root directory of the repo, run the following commands.

mkdir build
cd build
cmake ..
make
./uniquePtrExample

Output

##  Derived class pointer construction  ##

This should call Circle::Circle()
  Shape::Shape()
  Circle::Circle()
This should call Square::Square()
  Shape::Shape()
  Square::Square()

##  Base class pointer construction using make_unique<Derived>  ##

This should call Circle::Circle()
  Shape::Shape()
  Circle::Circle()
This should call Square::Square()
  Shape::Shape()
  Square::Square()

##  make_unique<Derived> using copy constructor  ##

This should call Circle::Circle(const Circle&)
  Shape::Shape(const Shape&)
  Circle::Circle(const Circle&)
This should call Square::Square(const Square&)
  Shape::Shape(const Shape&)
  Square::Square(const Square&)

##  Creating a vector of unique_ptrs to shapes and derived classes  ##

Shape::Shape()
Circle::Circle()
Shape::Shape()
Square::Square()
Shape::Shape()
Circle::Circle()

##  Cloning the middle pointer  ##

Shape::Shape(const Shape&)
Square::Square(const Square&)

##  The result of this range-based for loop should be 1,4,1,4  ##

  1,4,1,4

! DESTRUCTION !

Circle::~Circle()
Shape::~Shape()

Square::~Square()
Shape::~Shape()

Circle::~Circle()
Shape::~Shape()

Square::~Square()
Shape::~Shape()

Square::~Square()
Shape::~Shape()

Circle::~Circle()
Shape::~Shape()

Square::~Square()
Shape::~Shape()

Circle::~Circle()
Shape::~Shape()

Square::~Square()
Shape::~Shape()

Circle::~Circle()
Shape::~Shape()

About

Example on using std::unique_ptr from c++17 and how copy-constructors work

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published