クラス名
クラス宣言はプログラムにクラス名またはクラス型と呼ばれる新しい型を導入します。事前宣言を除きこれらのクラス宣言は特定の翻訳単位のクラス定義として機能します。翻訳単位に対して特定のクラス型の 1 種類の定義である場合があります。これらの新しいクラス型を使用してオブジェクトの宣言と互換性のないコンパイラは型のオブジェクトで操作が実行されないことを確認するには型チェックを実行できます。
解説
このような型チェックの例を次に示します。:
// 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;
}
前のコードに示すようにクラス型のオブジェクトの操作 (渡す割り当てや引数チェックなどの組み込み型のオブジェクト) 同じ型チェックが適用されます。
コンパイラがクラス型を区別するためクラスの型引数または組み込みの型引数に基づいてオーバーロードできます。オーバーロードされた関数の詳細については関数オーバーロード と オーバーロード を参照してください。