Software Design (C++) - Exercises V  (1 - 4),   4 - 5 Dec , 2014   


  1. Explain what is the meaning of 'this' pointer in C++ classes. How would you use 'this' in optimizing the assignment operator implementation of a user-defined class? Give an example in code.

  2. (a) Explain what is meant by RAII: "Resource Acquisition Is Initialization". Give an example of the use of RAII in a C++ program. Compare RAII with the use of try-catch-finally blocks in other languages (Java, C#).
     
    (b) What does it mean to say than a function gives basic exception guarantee? Strong exception guarantee? Nofail guarantee? Explain these concepts, their motivations, and respective trade-offs. E.g., note that the basic guarantee allows an object to have an unpredictable unknown state after a failed operation. How can such a guarantee be relevant or necessary? Give an illustrative example for each level of exception safety.

  3. (a) Find out what are std::unique_ptr and std::shared_ptr. What are they used for? What is the problem that they solve? Give some code examples of their use.

    (b) A function creates an object of class X dynamically and needs to return a pointer to that object back to the caller. How can this be implemented using the smart pointer types mentioned above? Is it OK to return a copy of a std::unique_ptr<X> or should you always return a shared pointer?

  4. Go through the container types of STL (for example, at http://en.cppreference.com/w/cpp/container). For each container, explain the following:
     
    (a) What kind of data processing tasks and needs is the container best suited for? Has it been optimized for certain kinds of operations (which)?
     
    (b) Study the documentation of some key operation of the container. What is the complexity of the operation (in relation with the amount of data in the containter)?