Skip to content

Improving Software Quality

Conversions in C++

Exercice 1: Implicit Conversions

Given the code below, determine the value of ui32 at the end of the program.

uint32_t ui32 = std::numeric_limits<uint32_t>::max();
float f = ui32;
ui32 = f;

Exercice 2: Implicit Conversions

Given the code below, determine the values of ui32 and i16 at the end of the program.

int32_t i32 = std::numeric_limits<int32_t>::min();
ui32 = i32;
int16_t i16 = i32;

Exercice 3: User-Defined Conversions

Given the following String class:

class String {
public:
  // constructors
  String(const char* szArray) {
    this->szArray = new char[strlen(szArray)];
    strcpy(this->szArray, szArray);
  }
  String(int size) {
    szArray = new char[size]{0};
  }
  // destructor
  virtual ~String() {
    if (this->szArray != nullptr) {
        delete[] this->szArray;
        this->szArray = nullptr;
    }
  }
private:
  char* szArray = nullptr;
};
determine if the following program compiles. If not, fix the error and determine the program’s behavior. If the program compiles, determine whether it does really what is expected. If it is not the case, fix the String class and modify the program accordingly.
int main() {
  String* s1 = new String("s1");
  String s2("s2");
  String* s3 = new String(10);
  String s4 = 10;
  String s5 = '1234';
  return 0;
}

Exercice 4: Constructors, Assignments, and Destructors

The constructors, assignment operators and destructors control the lifecycle of objects: creation, copy, move, and destruction. They must therefore be defined carefully.

For each C++ class X, these are default operations:

  • a default constructor: X()
  • a copy constructor: X(const X&)
  • a copy assignment: operator=(const X&)
  • a move constructor: X(X&&)
  • a move assignment: operator=(X&&)
  • a destructor: ~X()

By default, the compiler defines each of these operations if it is used, but the default can be suppressed or overriden.

The String class from the previous exercise defines two constructors and the destructor. This means that the compiler will not provide a default constructor or destructor. The other functions are defined by default. To understand whether the class is well defined, analyze the behavior of the following program:

int main() {
  String s1("s1");
  {
    String s2 = s1;
  }
  return 0;
}

Exercice 5: Constructors, Assignments, and Destructors

Given the modified String class from the previous exercise, analyze whether the code below produces the expected result.

int main() {
  String s1("s1");
  {
    String s2("");
    s2 = s1;
  }
  return 0;
}

Exercice 6: Constructors, Assignments, and Destructors

Imagine that you want to implement a different behavior when copying and assigning String instances, and that you want to move the ownership of the character array to the other instance rather than copying it.

Describe how you would modify the String class and the main() program for the previous two exercises.

Hint: You need to remove the definitions of the copy constructor and assignment operator because they implement a different behavior. Then, define a move constructor and a move assignment operator. This will implicitly define the copy constructor and assignment operator as deleted and the main() program will not compile anymore. You will then need to modify the main() program accordingly.