Implementing a string class in C++

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 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's not even interested in move operations. I doubt he knows about them. That's a different matter anyways.

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 became very uncomfortable and 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?

At that 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.
Needless to say, what was outcome of the interview as most of the time was lost in that argument itself.

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.

I would like to know how one should act in such situations. Just give up, or feel better that you don't have to work with such a project/team lead?


Category: c# Time: 2016-07-28 Views: 0

Related post

  • Implementing a String class using reference count 2016-02-21

    Design a STRING class. Provide support for: Assigning one object for another, Two string can be concatenated using + operator, Two strings can be compared using the relational operators. Modify the STRING class so that assigning/initializing a string

  • Implementing a C++11 raw string class 2015-01-06

    I'm trying to implement a RawString class that conforms to C++11: #include <iostream> #include <string> #include <memory> class RawString { private: char* buff_; public: RawString() { std::cout << "rawstring def ctor\n";

  • String class based on graphemes? 2010-10-20

    I'm wondering why we don't have some string classes that represent a string of Unicode grapheme clusters instead of code points or characters. It seems to me that in most applications it would be easier for programmers to access components of a graph

  • Why are there so many string classes in the face of std::string? 2012-06-05

    It seems to me that many bigger C++ libraries end up creating their own string type. In the client code you either have to use the one from the library (QString, CString, fbstring etc., I'm sure anyone can name a few) or keep converting between the s

  • Is using Interface implemented by single class a not recommended practice? 2012-08-07

    Isn't the whole point of an interface to for multiple classes to adhere to a set of rules and implementations? --------------Solutions------------- Strictly speaking, no you don't, YAGNI applies. That said, the time you'll spend creating the interfac

  • Java class to enforce an implementation of super classes' public methods 2013-08-01

    I have the following : public Class A { public void methodA() { .... } public void methodB() { .... } } I don't have control over Class A. I would like to create subclasses of Class A with a restriction that it must override methodA() (super.methodA(

  • Review of three constructors for a String class 2013-10-31

    I have the following declaration in my String.h file: private: char* nstring; int nlength; }; The following are three constructors I've implemented in String.cpp. I would like these to be reviewed. // Default constructor for this class. Initializes a

  • Example String Class in C 2014-10-16

    I wrote this small example piece of OOP being implemented in pure C. I wanted it to be reviewed for the following points: Portability Performance And especially Usability "How does it look" Implementation: typedef char XCFString; struct XCFStrin

  • Mutable String Class 2014-12-22

    Here is a String class that I've made for fun in my spare time, for myself. I have a few concerns about it, and am considering going immutable or a mix of the two where the string would be immutable when initialized from a constant, and then mutable

  • c_str implemenation for String class 2016-01-15

    I am reading chapter 12 in the Accelerated C++ book on implementing string class. There is a end-of-chapter question to implement the c_str() function. I am looking for some ideas. Here is what I have so far: My first attempt was to heap allocate a c

  • No matching constructor when I overload operator for dynamic string class 2016-02-03

    I get a problem with my String class. Constructor and copy constructor: private: char * buf; public: String( const char * s = "") { buf = strdup(s); } String( String &s) : buf(strdup(s.buf)) { } The operator: String operator + ( const String

  • The correct way to write a string class 2010-08-06

    Mentioned in : It's a really good idea to use an actual string class and not just char * everywhere or you're going to be in a world of pain. I currently use char *

  • What should I do in C++ when implementing a container class: storing objects by value or by reference? 2014-10-05

    I'm new to C++, coming from Java. In Java, all variables (except for primitives) are essentially pointers. They hold the address of whatever they're 'holding'. So any Java data structure stores it's data by reference. You can also store by value, i.e

  • Defining static const std::string class variable 2016-01-15

    How to define static const std::string class variable which can be used everywhere in my program safely? 1st approach - fails static initialization order fiasco: file: Consts.h namespace constants { struct Consts { static const std::string kVar = "12

  • Implementing a Time class in c++ 2016-01-28

    Design a class TIME which stores hour, minute and second. The class should have the methods to support the following: User may give the time value in 24-hour format. User may give the time value in AM/PM format Display the time in 24-hour format. Dis

  • Comparing a string of 6 digits to 6 char variables using NO methods from the string class 2016-01-29

    "You are not allowed to use any methods from the String class for finding matches. You may and should use the isDigit() method. The isDigit() method is a static method in the Character class. Failure to observe this constraint will result in a signif

  • How to skip empty lines without string class 2016-02-13

    In the following code I can't use the std::string class. I'm trying to read a file that has empty lines, and I want to ignore the empty lines when I encounter them. I've tested out some ideas using a simplified example and a made-up text file contain

  • Bundling CSS ad JS when global.asax is inhering the implementation from a class library 2016-02-15

    In my ASP.NET Webforms project, my global.asax inherits it's implementation from a class library. Now I would like to implement Bundling and magnification of JS and CSS for my Web project. Few options that I can think of Create a common shared folder

  • Is it bad practice when an interface method implemented by a class does not use all its parameters? 2016-07-24

    Should an interface method implemented by a class always use all parameters? By interface I do not necessarily mean actual interfaces, these could be other (abstract) classes which are extended, too. I sometimes find it hard to use all parameters in

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 1.982 (s). 13 q(s)