DEFAULT CLASS MEMBERS

It is useful to keep in mind everything that the compiler may supply by default for a class. Suppose you define a class like this:

class MyClass
{
  int data;
};

With just a single data member defined, you might imagine you can do very little with this class, but the compiler contributes some members. If you don’t specify them, the compiler will supply definitions for:

  • A default constructor:
    MyClass(){}
  • A copy constructor that does member-by-member copying:
    MyClass(const MyClass& obj) {/* Copy members */}
  • A destructor defined as:
    ~MyClass(){}
  • A default assignment operator that does member-by-member copying:
    MyClass& operator=(const MyClass& obj) {/* Copy members */}
WARNING Visual C++ does not implement automatically defined move constructors and move assignment operators which is not consistent with the C++ 11 language standard.

If you define any constructor, the default constructor will not be supplied. If you don’t want the other default operations to be in effect, you must define them in your class and make them private so they cannot be accessed from outside of the class.

Get Ivor Horton's Beginning Visual C++ 2012 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.