Memory Ownership and Smart Pointers

Unique Pointers to Const Objects

Is it possible to create a unique pointer to a const object in C++?

Abstract art representing computer programming

Yes, it's absolutely possible to create a std::unique_ptr to a const object in C++. This can be useful when you want to ensure that the object pointed to by the unique pointer cannot be modified through that pointer, while still maintaining the benefits of automatic memory management provided by std::unique_ptr.

Here's how you can create a unique pointer to a const object:

#include <iostream>
#include <memory>

int main() {
  auto constPtr = std::make_unique<const int>(42);  

  std::cout << "Value: " << *constPtr;

  // This would cause a compilation error:
  // *constPtr = 10; 
}
Value: 42

In this example, constPtr is a std::unique_ptr<const int>, pointing to a const int. We can read the value, but we can't modify it through this pointer.

You can also create a const unique pointer to a non-const object:

#include <iostream>
#include <memory>

int main() {
  const auto ptr = std::make_unique<int>(42); 

  std::cout << "Value: " << *ptr << '\n';

  // This would cause a compilation error:
  // ptr = std::make_unique<int>(10); 

  // But this is allowed:
  *ptr = 10;

  std::cout << "New value: " << *ptr << '\n';
}
Value: 42
New value: 10

Here, ptr is const (can't be reassigned), but the int it points to isn't const (can be modified).

You can even have a const unique pointer to a const object:

#include <iostream>
#include <memory>

int main() {
  const auto constPtr =
      std::make_unique<const int>(42);  

  std::cout << "Value: " << *constPtr;

  // These would cause compilation errors:
  // constPtr =  std::make_unique<const int>(10); 
  // *constPtr = 10; 
}
Value: 42

In this case, neither the pointer nor the pointed-to object can be modified.

Remember, const-correctness is a powerful tool in C++ for preventing unintended modifications and communicating intent in your code. Using const with unique pointers allows you to leverage both the safety of const and the automatic memory management of smart pointers.

When working with classes, const unique pointers are particularly useful:

#include <iostream>
#include <memory>
#include <string>

class Character {
public:
  Character(std::string name)
    : name(std::move(name)) {}

  std::string getName() const { return name; }

  void rename(const std::string& newName) {
    name = newName;
  }

private:
  std::string name;
};

int main() {
  auto constCharPtr =
    std::make_unique<const Character>("Frodo");

  std::cout << "Name: " << constCharPtr->getName();

  // This would cause a compilation error:
  // constCharPtr->rename("Gandalf"); 
}
Name: Frodo

In this example, constCharPtr is a unique pointer to a const Character. We can call const member functions like getName(), but we can't call non-const member functions like rename().

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