Home > Articles > Programming > C/C++

  • Print
  • + Share This
This chapter is from the book

9.5 Values and Objects

We can divide C++ types into two categories: value types and object types.

Instances of value types are relatively "simple": They occupy contiguous memory space, and can be copied or compared quickly. Examples of value types are Anything*, int, char, QString, QDate, and QVariant.

Instances of object types, on the other hand, are typically more complex and maintain some sort of identity. Object types are rarely copied (cloned). If cloning is permitted, the operation is usually expensive and results in a new object (graph) that has a separate identity from the original.

The designers of QObject asserted an unequivocal "no copy" policy by designating its assignment operator and copy constructor private. This effectively prevents the compiler from generating assignment operators and copy constructors for QObject-derived classes. One consequence of this scheme is that any attempt to pass or return QObject-derived classes by value to or from functions results in a compile-time error.

Exercises: QObject

  1. Rewrite the Contact and ContactList from "Exercise: Contact List" in Chapter 4 so that they both derive from QObject.

    When a Contact is to be added to a ContactList, make the Contact the child of the ContactList.

  2. Port the client code you wrote for "Exercise: Contact List" to use the new versions of Contact and ContactList.

  • + Share This
  • 🔖 Save To Your Account