共用方式為


類別名稱

類別宣告會引入新的型別,稱為 [執行程式的 [類別名稱或類別型別。 除了為順向宣告,這些類別宣告也做為指定的轉譯單元類別的定義。 可能只有一個轉譯單位每給定的類別型別定義。 使用這些新的類別型別,您可以宣告物件,而且編譯器可以執行檢查,以確認型別與不相容的作業會在物件上執行的型別。

備註

這種型別檢查的範例是:

// 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;
}

如前面的程式碼所示,作業 (例如工作分派和引數傳遞的類別型別物件) 受限於相同的型別檢查當做內建型別的物件。

因為編譯器會區別類別型別,則可多載函式為基礎的類別型別引數,以及內建的型別引數。 如需有關多載函式的詳細資訊,請參閱函式多載化多載化

請參閱

參考

類別、 結構和等位