I recently attended an interview for a C++ position in a very reputed bank.   The interviewer started off by asking me to implement a string class but was only interested in constructor, copy-constructor, assignment operator and destructor.Basically, he wanted to know how I would implement Rule of Three and nothing else.  He was not even interested in move operations. So, I implemented the assignment operator in terms of copy constructor: class String{  /*....*/String& operator=(String other){   std::swap(m_data, other.m_data);   return *this;}private:   char* m_data;};   Since this is pretty common way to implement assignment operator, I thought he would like it. The problem started with that.He questioned why I implement it that way. When I explained that this is exception safe way of implementing it, he asked what's wrong in implementing it:String& operator=(const String& other){   if (this != &other)   {     delete[] m_data;     m_data = new char[strlen(other.m_data) + 1] {'\0'};     strcpy(m_data, other.m_data);     return *this;   } }   His argument was:We are about to overwrite the data anyways so what's wrong in deleting first even if it's not exception safe?In the moment it did not occur to me that code will crash in the event of an exception but I did try to argue that internal data should retain original values in the event of an exception.Had he argued on the basis that copy-and-swap will always do reallocation even if there is enough size, etc, I would have felt happy. But his argument was that he wants performance at any cost. Can somebody help me with a comparative review of these two operator= implementations so that I am in a better position to argue my case / understand the interviewer's position if I encounter similar situations in the future?
Implementing a string class in C++
c++;strings;interview questions;reinventing the wheel;comparative review