ProgrammingHardGATE CS · TCS NQT Prime · Product Company SDE

What does the 'Rule of Five' in modern C++ say?

  1. A. It is a naming convention requiring getter/setter pairs
  2. B. If a class needs a custom destructor, copy constructor, or copy assignment operator, it typically needs to define all five: destructor, copy constructor, copy assignment, move constructor and move assignment
  3. C. A class is limited to at most five virtual functions
  4. D. Any class with five or more data members automatically needs custom copy logic

Correct answer: B. If a class needs a custom destructor, copy constructor, or copy assignment operator, it typically needs to define all five: destructor, copy constructor, copy assignment, move constructor and move assignment

Once a class manages a resource and needs any one of destructor/copy-constructor/copy-assignment written by hand, it usually needs all five special member functions defined consistently, including the two move operations.

More OOP with C++ and Java questions

Exams that ask this topic