Unique Pointers to Const Objects

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

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().

Memory Ownership and Smart Pointers

Learn how to manage dynamic memory using unique pointers and the concept of memory ownership

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Performance of Unique Pointers
What's the performance overhead of using unique pointers compared to raw pointers in C++?
Deleting Raw Pointers from Unique Pointers
What happens if I try to delete the raw pointer obtained from the get() method of a unique pointer?
Thread Safety of Unique Pointers
Is it safe to use unique pointers in multithreaded applications?
Reset vs Release for Unique Pointers
What's the difference between reset() and release() for unique pointers?
Unique Pointers with C-style APIs
Can I use unique pointers with C-style APIs that expect raw pointers?
Returning Unique Pointers from Functions
What's the best way to return a unique pointer from a function?
Copyable Classes with Unique Pointers
How can I use unique pointers in a class that needs to be copyable?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant