Memory Ownership and Smart Pointers

Copyable Classes with Unique Pointers

How can I use unique pointers in a class that needs to be copyable?

Abstract art representing computer programming

Using std::unique_ptr in a class that needs to be copyable presents a challenge because unique_ptr is not copyable by design. However, there are several strategies you can employ depending on your specific needs:

1. Deep Copy

If you want each copy of your class to have its own independent copy of the managed object, you can implement a deep copy:

#include <memory>
#include <string>

class Weapon {
public:
  std::string Name;

  Weapon(const std::string& Name)
    : Name{Name}{}
};

class Character {
  std::unique_ptr<Weapon> WeaponPtr;
  std::string Name;

public:
  Character(const std::string& Name,
            const std::string& WeaponName)
    : WeaponPtr{
        std::make_unique<Weapon>(WeaponName)},
      Name{Name}{}

  // Copy constructor 
  Character(const Character& Other)
    : WeaponPtr{
        Other.WeaponPtr
          ? std::make_unique<Weapon>(
            *Other.WeaponPtr)
          : nullptr},
      Name{Other.Name}{}

  // Copy assignment operator 
  Character& operator=(const Character& Other){
    if (this != &Other) {
      WeaponPtr =
        Other.WeaponPtr
          ? std::make_unique<Weapon>(
            *Other.WeaponPtr)
          : nullptr;
      Name = Other.Name;
    }
    return *this;
  }

  // Move constructor and assignment operator...
};

This approach creates a new Weapon object for each copy of Character.

2. Shared Ownership

If you want multiple Character objects to share the same Weapon, consider using std::shared_ptr instead:

#include <memory>
#include <string>

class Weapon {
public:
  std::string Name;
};

class Character {
  std::shared_ptr<Weapon> WeaponPtr;
  std::string Name;

public:
  Character(const std::string& Name,
            const std::string& WeaponName)
    : WeaponPtr{
        std::make_shared<Weapon>(WeaponName)},
      Name{Name}{ }

  // Copy constructor and assignment operator
  // are automatically generated
};

This allows multiple Character objects to share ownership of the same Weapon.

3. Pointer to unique_ptr

If you want to maintain unique ownership but allow the pointer itself to be shared, you can use a pointer to a unique_ptr:

#include <memory>
#include <string>

class Weapon {
public:
  std::string Name;
};

class Character {
  std::unique_ptr<Weapon>* WeaponPtr;
  std::string Name;

public:
  Character(const std::string& Name,
            std::unique_ptr<Weapon>& Weapon)
    : WeaponPtr{&Weapon},
      Name{Name}{ }

  // Copy constructor and assignment operator
  // can now be generated automatically
};

int main(){
  auto Sword{std::make_unique<Weapon>("Sword")};
  Character Aragorn{"Aragorn", Sword};

  // This is now possible
  Character AragornCopy{Aragorn};
}

Be cautious with this approach, as it can lead to dangling pointers if the original unique_ptr is destroyed.

4. Optional Copying

You might decide that copying should be allowed, but the copied object doesn't get the unique_ptr:

#include <memory>
#include <optional>
#include <string>

class Weapon {
public:
  std::string Name;
};

class Character {
  std::unique_ptr<Weapon> WeaponPtr;
  std::string Name;

public:
  Character(const std::string& Name,
            const std::string& WeaponName)
    : WeaponPtr{
        std::make_unique<Weapon>(WeaponName)},
      Name{Name}{}

  Character(const Character& Other)
    : WeaponPtr{nullptr}, Name{Other.Name}{}

  std::optional<std::string>
  GetWeaponName() const{
    return WeaponPtr
             ? std::optional<std::string>{
               WeaponPtr->Name}
             : std::nullopt;
  }
};

In this approach, copied Character objects don't have a weapon, but you can still access the weapon name if it exists.

5. Clone Method

Instead of making the class itself copyable, you could provide a Clone() method:

#include <memory>
#include <string>

class Weapon {
public:
  std::string Name;
};

class Character {
  std::unique_ptr<Weapon> WeaponPtr;
  std::string Name;

public:
  Character(const std::string& Name,
            const std::string& WeaponName)
    : WeaponPtr{
        std::make_unique<Weapon>(WeaponName)},
      Name{Name}{}

  std::unique_ptr<Character> Clone() const{
    auto NewCharacter{
      std::make_unique<Character>(Name, "")};
    if (WeaponPtr) {
      NewCharacter->WeaponPtr =
        std::make_unique<Weapon>(*WeaponPtr);
    }
    return NewCharacter;
  }
};

This allows users to explicitly create copies when needed, while keeping the class non-copyable by default.

Each of these approaches has its pros and cons, and the best choice depends on your specific use case and requirements. Consider the semantics you want for your Character class and choose the approach that best fits your needs.

Answers to questions are automatically generated and may not have been reviewed.

3D art showing a progammer setting up a development environment
Part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access

This course includes:

  • 57 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved