Partager via


Class Names

Class declarations introduce new types, called class names or class types, into programs. Except for forward declarations, these class declarations also act as definitions of the class for a given translation unit. There may be only one definition for a given class type per translation unit. Using these new class types, you can declare objects, and the compiler can perform type checking to verify that no operations incompatible with the types are performed on the objects.

Remarks

An example of such type checking is:

// class_names.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;
class Point {
public:
   unsigned x, y;
};

class Rect {
public:
   unsigned x1, y1, x2, y2;
};

// Prototype a function that takes two arguments, one of type
//  Point and the other of type pointer to Rect.
int PtInRect( Point, Rect & );

int main() {
   Point pt;
   Rect  rect;

   rect = pt;   // C2679 Types are incompatible.
   pt = rect;   // C2679 Types are incompatible.

  // Error. Arguments to PtInRect are reversed.
  // cout << "Point is " << PtInRect( rect, pt ) ? "" : "not"
  //   << " in rectangle" << endl;
}

As the preceding code illustrates, operations (such as assignment and argument passing) on class-type objects are subject to the same type checking as objects of built-in types.

Because the compiler distinguishes between class types, functions can be overloaded on the basis of class-type arguments as well as built-in type arguments. For more information about overloaded functions, see Function Overloading and Overloading.

See Also

Concepts

Classes, Structures, and Unions