User Defined Conversions

Preventing Constructor Calls

How can we use delete to prevent specific constructor calls?

Abstract art representing computer programming

In C++, you can use the delete keyword to prevent specific constructor calls, ensuring that certain conversions or initializations are not allowed.

This is particularly useful for preventing conversions that don’t make logical sense or could introduce bugs.

For example, let’s prevent a Vector class from being constructed using a bool:

#include <iostream>

class Vector {
 public:
  float x, y, z;

  // Constructor from float is allowed
  explicit Vector(float value)
    : x(value), y(value), z(value) {}

  // Constructor from bool is deleted
  Vector(bool) = delete;  
};

void Move(Vector direction) {
  std::cout << "Moving in direction: "
    << direction.x << ", "
    << direction.y << ", "
    << direction.z << "\n";
}

int main() {
  Vector v1(1.0f);

  // Moving in direction: 1, 1, 1
  Move(v1); 

  // Error: constructor is deleted
  Vector v2(true); 

  // Error: constructor is deleted
  Move(true); 
}
error: attempting to reference a deleted function
note: 'Vector::Vector(bool)': function was explicitly deleted

Steps to Delete Specific Constructor Calls

  1. Define the Class: Create your class and define its members and constructors.
  2. Delete the Constructor: Use the delete keyword to specify which constructor you want to disable.

Explanation

  • Class Definition: The Vector class has three float members: x, y, and z.
  • Allowed Constructor: The constructor that takes a float is explicitly defined and allowed.
  • Deleted Constructor: The constructor that takes a bool is marked with = delete. This prevents any code from using this constructor, ensuring that a Vector cannot be created with a bool.

Usage in Code

In the main() function, creating a Vector with a float works as expected. However, attempting to create a Vector with a bool results in a compilation error.

Benefits of Using delete

  • Error Prevention: Prevents unintended and potentially harmful conversions or initializations.
  • Code Clarity: Makes it clear which operations are valid and which are not, improving code readability and maintainability.
  • Safety: Ensures that objects are only created in ways that make logical sense, reducing the likelihood of bugs.

By using delete, you can fine-tune the behavior of your classes, ensuring that they are used correctly and safely. This technique is particularly valuable in large codebases where unintended conversions can lead to subtle and hard-to-find bugs.

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

A computer programmer
Part of the course:

Professional C++

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

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% 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