How can you prevent an object of a C++ class from being copied?
- Use private constructors
- Make the class abstract
- Declare the copy constructor as private
- Use an interface
By declaring the copy constructor (and the assignment operator) as private, you ensure that they cannot be invoked from outside the class, effectively preventing the creation of a copy of an object of the class. This is a common technique for the Singleton pattern.
Loading...
Related Quiz
- The function _______ is used to merge two sorted ranges in the C++ STL.
- When passing parameters by reference, which symbol is used to denote reference in C++?
- What is the main purpose of using friend functions in C++?
- Consider a scenario where you have a large dataset and you need to frequently erase and insert elements in the middle of the data. Which STL container should be avoided in order to prevent frequent reallocations and data movements?
- The function _______ allows you to swap the elements of two containers in C++ STL.