CObject
クラスCObject
Class
MFC ライブラリの重要な基底クラスです。The principal base class for the Microsoft Foundation Class Library.
構文Syntax
class AFX_NOVTABLE CObject
メンバーMembers
プロテクト コンストラクターProtected Constructors
名前Name | 説明Description |
---|---|
CObject::CObject |
既定のコンストラクターです。Default constructor. |
パブリック メソッドPublic Methods
名前Name | 説明Description |
---|---|
CObject::AssertValid |
このオブジェクトの整合性を検証します。Validates this object's integrity. |
CObject::Dump |
このオブジェクトの診断ダンプを生成します。Produces a diagnostic dump of this object. |
CObject::GetRuntimeClass |
CRuntimeClass このオブジェクトのクラスに対応する構造体を返します。Returns the CRuntimeClass structure corresponding to this object's class. |
CObject::IsKindOf |
このオブジェクトと特定のクラスとの関係をテストします。Tests this object's relationship to a given class. |
CObject::IsSerializable |
このオブジェクトをシリアル化できるかどうかをテストします。Tests to see whether this object can be serialized. |
CObject::Serialize |
アーカイブからのオブジェクトの読み込みまたは保存を行います。Loads or stores an object from/to an archive. |
パブリック演算子Public Operators
名前Name | 説明Description |
---|---|
CObject::operator delete |
特別な delete 演算子。Special delete operator. |
CObject::operator new |
特別な new 演算子。Special new operator. |
RemarksRemarks
これは、やなどのライブラリクラスだけでなく、記述するクラスのルートとしても機能し CFile
CObList
ます。It serves as the root not only for library classes such as CFile
and CObList
, but also for the classes that you write. CObject
基本的なサービスを提供します。CObject
provides basic services, including
- シリアル化のサポートSerialization support
- ランタイムクラス情報Run-time class information
- オブジェクトの診断出力Object diagnostic output
- コレクションクラスとの互換性Compatibility with collection classes
CObject
は複数の継承をサポートしていません。CObject
doesn't support multiple inheritance. 派生クラスは基底クラスを1つだけ持つことができ、 CObject
CObject
階層の最上位にある必要があります。Your derived classes can have only one CObject
base class, and that CObject
must be leftmost in the hierarchy. ただし、構造と非 CObject
派生クラスを右側の複数継承分岐に含めることは許可されています。It's permissible, however, to have structures and non- CObject
-derived classes in right-hand multiple-inheritance branches.
CObject
クラスの実装と宣言で省略可能なマクロの一部を使用すると、派生の大きな利点が得られます。You'll realize major benefits from CObject
derivation if you use some of the optional macros in your class implementation and declarations.
第1レベルのマクロ、 DECLARE_DYNAMIC
および IMPLEMENT_DYNAMIC
では、階層内のクラス名とその位置への実行時アクセスが許可されます。The first-level macros, DECLARE_DYNAMIC
and IMPLEMENT_DYNAMIC
, permit run-time access to the class name and its position in the hierarchy. これにより、意味のある診断ダンプを行うことができます。This, in turn, allows meaningful diagnostic dumping.
第2レベルのマクロとには、 DECLARE_SERIAL
IMPLEMENT_SERIAL
第1レベルのマクロのすべての機能が含まれています。また、"アーカイブ" との間でオブジェクトを "シリアル化" できるようにします。The second-level macros, DECLARE_SERIAL
and IMPLEMENT_SERIAL
, include all the functionality of the first-level macros, and they enable an object to be "serialized" to and from an "archive."
一般的な、およびを使用した Microsoft Foundation classes と C++ クラスの派生の詳細について CObject
は、「 CObject と シリアル化の使用」を参照してください。For information about deriving Microsoft Foundation classes and C++ classes in general and using CObject
, see Using CObject and Serialization.
継承階層Inheritance Hierarchy
CObject
必要条件Requirements
ヘッダー:afx.h
Header: afx.h
CObject:: AssertValidCObject::AssertValid
このオブジェクトの整合性を検証します。Validates this object's integrity.
virtual void AssertValid() const;
RemarksRemarks
AssertValid
内部状態を確認することにより、このオブジェクトの有効性チェックを実行します。AssertValid
performs a validity check on this object by checking its internal state. ライブラリのデバッグバージョンでは、は AssertValid
アサートしてから、アサーションが失敗した行番号とファイル名を一覧表示するメッセージを使用してプログラムを終了する場合があります。In the Debug version of the library, AssertValid
may assert and then terminate the program with a message that lists the line number and filename where the assertion failed.
独自のクラスを記述する場合は、関数をオーバーライドして、 AssertValid
自分やクラスの他のユーザーに診断サービスを提供する必要があります。When you write your own class, you should override the AssertValid
function to provide diagnostic services for yourself and other users of your class. 通常、オーバーライドされたは、 AssertValid
AssertValid
派生クラスに固有のデータメンバーをチェックする前に、基本クラスの関数を呼び出します。The overridden AssertValid
usually calls the AssertValid
function of its base class before checking data members unique to the derived class.
は関数であるため AssertValid
const
、テスト中にオブジェクトの状態を変更することはできません。Because AssertValid
is a const
function, you aren't permitted to change the object state during the test. 独自の派生クラス AssertValid
関数は、例外をスローするのではなく、無効なオブジェクトデータを検出するかどうかをアサートする必要があります。Your own derived class AssertValid
functions shouldn't throw exceptions but rather should assert whether they detect invalid object data.
"有効性" の定義は、オブジェクトのクラスによって異なります。The definition of "validity" depends on the object's class. ルールとして、関数は "浅いチェック" を行う必要があります。As a rule, the function should do a "shallow check." つまり、オブジェクトに他のオブジェクトへのポインターが含まれている場合は、ポインターが存在しないかどうかを確認する必要があり NULL
ますが、ポインターによって参照されているオブジェクトに対して有効性テストを実行することはできません。That is, if an object contains pointers to other objects, it should check to see whether the pointers aren't NULL
, but it shouldn't do validity testing on the objects referred to by the pointers.
例Example
CObList::CObList
CAge
すべての例で使用されるクラスの一覧については、「」を参照してください CObject
。See CObList::CObList
for a listing of the CAge
class used in all CObject
examples.
void CAge::AssertValid() const
{
CObject::AssertValid();
ASSERT(m_years > 0);
ASSERT(m_years < 105);
}
別の例については、「」を参照してください AfxDoForAllObjects
。For another example, see AfxDoForAllObjects
.
CObject:: CObjectCObject::CObject
これらの関数は、標準 CObject
コンストラクターです。These functions are the standard CObject
constructors.
CObject();
CObject(const CObject& objectSrc);
パラメーターParameters
objectSrcobjectSrc
別のへの参照 CObject
A reference to another CObject
RemarksRemarks
既定のバージョンは、派生クラスのコンストラクターによって自動的に呼び出されます。The default version is automatically called by the constructor of your derived class.
クラスがシリアル化可能である (マクロが組み込まれている) 場合は IMPLEMENT_SERIAL
、クラス宣言に既定のコンストラクター (引数なしのコンストラクター) が必要です。If your class is serializable (it incorporates the IMPLEMENT_SERIAL
macro), then you must have a default constructor (a constructor with no arguments) in your class declaration. 既定のコンストラクターが不要な場合は、プライベートまたは保護された "空の" コンストラクターを宣言します。If you don't need a default constructor, declare a private or protected "empty" constructor. 詳細については、「 Using CObject
」を参照してください。For more information, see Using CObject
.
標準 C++ の既定のクラスコピーコンストラクターは、メンバーごとのコピーを行います。The standard C++ default class copy constructor does a member-by-member copy. プライベートコピーコンストラクターが存在することは、 CObject
クラスのコピーコンストラクターが必要であるが使用できない場合に、コンパイラエラーメッセージを保証します。The presence of the private CObject
copy constructor guarantees a compiler error message if the copy constructor of your class is needed but not available. クラスにこの機能が必要な場合は、コピーコンストラクターを指定します。Provide a copy constructor if your class requires this capability.
例Example
CObList::CObList
CAge
例で使用されているクラスの一覧については、「」を参照してください CObject
。See CObList::CObList
for a listing of the CAge
class used in the CObject
examples.
// Create a CAge object using the default constructor.
CAge age1;
// Create a CAge object using the copy constructor.
CAge age2(age1);
CObject::D umpCObject::Dump
オブジェクトのコンテンツをオブジェクトにダンプし CDumpContext
ます。Dumps the contents of your object to a CDumpContext
object.
virtual void Dump(CDumpContext& dc) const;
パラメーターParameters
修飾dc
ダンプの診断ダンプコンテキスト (通常は) afxDump
。The diagnostic dump context for dumping, usually afxDump
.
RemarksRemarks
独自のクラスを記述する場合は、関数をオーバーライドして、 Dump
自分やクラスの他のユーザーに診断サービスを提供する必要があります。When you write your own class, you should override the Dump
function to provide diagnostic services for yourself and other users of your class. 通常、オーバーライドされたは、 Dump
Dump
派生クラスに固有のデータメンバーを出力する前に、基本クラスの関数を呼び出します。The overridden Dump
usually calls the Dump
function of its base class before printing data members unique to the derived class. CObject::Dump
クラスでマクロまたはマクロを使用する場合は、クラス名を出力し IMPLEMENT_DYNAMIC
IMPLEMENT_SERIAL
ます。CObject::Dump
prints the class name if your class uses the IMPLEMENT_DYNAMIC
or IMPLEMENT_SERIAL
macro.
注意
Dump
関数は、出力の末尾に改行文字を出力しません。Your Dump
function shouldn't print a newline character at the end of its output.
Dump
呼び出しは、Microsoft Foundation Class ライブラリのデバッグバージョンでのみ意味があります。Dump
calls make sense only in the Debug version of the Microsoft Foundation Class Library. 呼び出し、関数宣言、および関数の実装は #ifdef _DEBUG
、 #endif
条件付きコンパイルのステートメントを使用して、かっこで囲む必要があります。You should bracket calls, function declarations, and function implementations with #ifdef _DEBUG
, #endif
statements for conditional compilation.
は関数であるため Dump
const
、ダンプ中にオブジェクトの状態を変更することはできません。Since Dump
is a const
function, you aren't permitted to change the object state during the dump.
CDumpContext
挿入 (<<) 演算子は、 Dump
ポインターが挿入されたときにを呼び出し CObject
ます。The CDumpContext
insertion (<<) operator calls Dump
when a CObject
pointer is inserted.
Dump
オブジェクトの "非循環" ダンプのみを許可します。Dump
permits only "acyclic" dumping of objects. たとえば、オブジェクトの一覧をダンプできますが、オブジェクトの1つがリスト自体の場合は、最終的にスタックをオーバーフローします。You can dump a list of objects, for example, but if one of the objects is the list itself, you'll eventually overflow the stack.
例Example
CObList::CObList
CAge
すべての例で使用されるクラスの一覧については、「」を参照してください CObject
。See CObList::CObList
for a listing of the CAge
class used in all CObject
examples.
void CAge::Dump(CDumpContext& dc) const
{
CObject::Dump(dc);
dc << _T("Age = ") << m_years;
}
CObject:: GetRuntimeClassCObject::GetRuntimeClass
CRuntimeClass
このオブジェクトのクラスに対応する構造体を返します。Returns the CRuntimeClass
structure corresponding to this object's class.
virtual CRuntimeClass* GetRuntimeClass() const;
戻り値Return Value
CRuntimeClass
このオブジェクトのクラスに対応する構造体へのポインター。 NULL
できません。A pointer to the CRuntimeClass
structure corresponding to this object's class; never NULL
.
RemarksRemarks
CRuntimeClass
各派生クラスには1つの構造体があり CObject
ます。There's one CRuntimeClass
structure for each CObject
-derived class. 構造体のメンバーは次のとおりです。The structure members are as follows:
LPCSTR m_lpszClassName
ASCII クラス名を格納している null で終わる文字列。LPCSTR m_lpszClassName
A null-terminated string containing the ASCII class name.int m_nObjectSize
オブジェクトのサイズ (バイト単位)。int m_nObjectSize
The size of the object, in bytes. 割り当てられたメモリを指すデータメンバーがオブジェクトに含まれている場合、そのメモリのサイズは含まれません。If the object has data members that point to allocated memory, the size of that memory isn't included.UINT m_wSchema
スキーマ番号 (シリアル化クラスの場合は-1)。UINT m_wSchema
The schema number ( -1 for nonserializable classes).IMPLEMENT_SERIAL
スキーマ番号の説明については、マクロを参照してください。See theIMPLEMENT_SERIAL
macro for a description of schema number.CObject* (PASCAL* m_pfnCreateObject)()
クラスのオブジェクトを作成する既定のコンストラクターへの関数ポインター (クラスが動的作成をサポートしている場合にのみ有効)。それ以外の場合、はを返しNULL
ます。CObject* (PASCAL* m_pfnCreateObject)()
A function pointer to the default constructor that creates an object of your class (valid only if the class supports dynamic creation; otherwise, returnsNULL
).CRuntimeClass* (PASCAL* m_pfn_GetBaseClass )()
アプリケーションが AFXDLL バージョンの MFC に動的にリンクされている場合は、CRuntimeClass
基本クラスの構造体を返す関数へのポインター。CRuntimeClass* (PASCAL* m_pfn_GetBaseClass )()
If your application is dynamically linked to the AFXDLL version of MFC, a pointer to a function that returns theCRuntimeClass
structure of the base class.CRuntimeClass* m_pBaseClass
アプリケーションが MFC に静的にリンクされている場合は、CRuntimeClass
基本クラスの構造体へのポインター。CRuntimeClass* m_pBaseClass
If your application is statically linked to MFC, a pointer to theCRuntimeClass
structure of the base class.
この関数 IMPLEMENT_DYNAMIC
では、クラスの IMPLEMENT_DYNCREATE
実装で、、またはマクロを使用する必要があり IMPLEMENT_SERIAL
ます。This function requires use of the IMPLEMENT_DYNAMIC
, IMPLEMENT_DYNCREATE
, or IMPLEMENT_SERIAL
macro in the class implementation. それ以外の場合は、正しくない結果が返されます。You'll get incorrect results otherwise.
例Example
CObList::CObList
CAge
すべての例で使用されるクラスの一覧については、「」を参照してください CObject
。See CObList::CObList
for a listing of the CAge
class used in all CObject
examples.
CAge a(21);
CRuntimeClass* prt = a.GetRuntimeClass();
ASSERT(strcmp(prt->m_lpszClassName, "CAge") == 0);
CObject:: IsKindOfCObject::IsKindOf
このオブジェクトと特定のクラスとの関係をテストします。Tests this object's relationship to a given class.
BOOL IsKindOf(const CRuntimeClass* pClass) const;
パラメーターParameters
pClass
CRuntimeClass
派生クラスに関連付けられている構造体へのポインター CObject
。A pointer to a CRuntimeClass
structure associated with your CObject
-derived class.
戻り値Return Value
オブジェクトがクラスに対応している場合は0以外の。それ以外の場合は0です。Nonzero if the object corresponds to the class; otherwise 0.
RemarksRemarks
この関数 pClass
は、(1) が指定されたクラスのオブジェクトであるか (2)、指定されたクラスから派生したクラスのオブジェクトであるかどうかをテストします。This function tests pClass
to see if (1) it's an object of the specified class or (2) it's an object of a class derived from the specified class. この関数は DECLARE_DYNAMIC
、、、またはマクロで宣言されたクラスに対してのみ機能 DECLARE_DYNCREATE
DECLARE_SERIAL
します。This function works only for classes declared with the DECLARE_DYNAMIC
, DECLARE_DYNCREATE
, or DECLARE_SERIAL
macro.
C++ のポリモーフィズム機能を損なうため、この関数は広く使用しないでください。Don't use this function extensively because it defeats the C++ polymorphism feature. 代わりに仮想関数を使用してください。Use virtual functions instead.
例Example
CObList::CObList
CAge
すべての例で使用されるクラスの一覧については、「」を参照してください CObject
。See CObList::CObList
for a listing of the CAge
class used in all CObject
examples.
CAge a(21); // Must use IMPLEMENT_DYNAMIC, IMPLEMENT _DYNCREATE, or
// IMPLEMENT_SERIAL
ASSERT(a.IsKindOf(RUNTIME_CLASS(CAge)));
ASSERT(a.IsKindOf(RUNTIME_CLASS(CObject)));
CObject:: IsSerializableCObject::IsSerializable
このオブジェクトがシリアル化の対象であるかどうかをテストします。Tests whether this object is eligible for serialization.
BOOL IsSerializable() const;
戻り値Return Value
このオブジェクトをシリアル化できる場合は0以外の。それ以外の場合は0です。Nonzero if this object can be serialized; otherwise 0.
RemarksRemarks
クラスをシリアル化できるようにするには、その宣言にマクロが含まれている必要があり DECLARE_SERIAL
ます。また、実装にマクロが含まれている必要があり IMPLEMENT_SERIAL
ます。For a class to be serializable, its declaration must contain the DECLARE_SERIAL
macro, and the implementation must contain the IMPLEMENT_SERIAL
macro.
注意
この関数はオーバーライドしないでください。Don't override this function.
例Example
CObList::CObList
CAge
すべての例で使用されるクラスの一覧については、「」を参照してください CObject
。See CObList::CObList
for a listing of the CAge
class used in all CObject
examples.
CAge a(21);
ASSERT(a.IsSerializable());
CObject:: operator deleteCObject::operator delete
ライブラリのリリースバージョンでは、演算子 delete
によって割り当てられたメモリが解放され new
ます。For the Release version of the library, operator delete
frees the memory allocated by operator new
.
void PASCAL operator delete(void* p);
void PASCAL operator delete(
void* p,
void* pPlace);
void PASCAL operator delete(
void* p,
LPCSTR lpszFileName,
int nLine);
RemarksRemarks
デバッグバージョンでは、演算子は delete
メモリリークを検出するように設計された割り当て監視スキームに参加します。In the Debug version, operator delete
participates in an allocation-monitoring scheme designed to detect memory leaks.
コード行を使用する場合If you use the code line
#define new DEBUG_NEW
の実装の前。CPP ファイルを使用すると、の3番目のバージョンが delete
使用され、後でレポートできるように、割り当てられたブロックにファイル名と行番号が格納されます。before any of your implementations in a .CPP file, then the third version of delete
will be used, storing the filename and line number in the allocated block for later reporting. 余分なパラメーターの指定について心配する必要はありません。これはマクロによって自動的に処理されます。You don't have to worry about supplying the extra parameters; a macro takes care of that for you.
デバッグモードでを使用しない場合でも DEBUG_NEW
、前に説明したソースファイルの行番号レポートがなくても、リーク検出は発生します。Even if you don't use DEBUG_NEW
in Debug mode, you still get leak detection, but without the source-file line-number reporting described above.
演算子とをオーバーライドした場合は new
delete
、この診断機能がプランます。If you override operators new
and delete
, you forfeit this diagnostic capability.
例Example
CObList::CObList
CAge
例で使用されているクラスの一覧については、「」を参照してください CObject
。See CObList::CObList
for a listing of the CAge
class used in the CObject
examples.
void CAge::operator delete(void* p)
{
free(p);
}
void CAge::operator delete(void *p, LPCSTR lpszFileName, int nLine)
{
UNREFERENCED_PARAMETER(lpszFileName);
UNREFERENCED_PARAMETER(nLine);
free(p);
}
CObject:: operator newCObject::operator new
ライブラリのリリースバージョンでは、演算子 new
はと同様の方法で最適なメモリ割り当てを行い malloc
ます。For the Release version of the library, operator new
does an optimal memory allocation in a manner similar to malloc
.
void* PASCAL operator new(size_t nSize);
void* PASCAL operator new(size_t, void* p);
void* PASCAL operator new(
size_t nSize,
LPCSTR lpszFileName,
int nLine);
RemarksRemarks
デバッグバージョンでは、演算子は new
メモリリークを検出するように設計された割り当て監視スキームに参加します。In the Debug version, operator new
participates in an allocation-monitoring scheme designed to detect memory leaks.
コード行を使用する場合If you use the code line
#define new DEBUG_NEW
の実装の前。CPP ファイルを使用すると、の2番目のバージョンが new
使用され、後でレポートを作成するために、割り当てられたブロックにファイル名と行番号が格納されます。before any of your implementations in a .CPP file, then the second version of new
will be used, storing the filename and line number in the allocated block for later reporting. 余分なパラメーターの指定について心配する必要はありません。これはマクロによって自動的に処理されます。You don't have to worry about supplying the extra parameters; a macro takes care of that for you.
デバッグモードでを使用しない場合でも DEBUG_NEW
、前に説明したソースファイルの行番号レポートがなくても、リーク検出は発生します。Even if you don't use DEBUG_NEW
in Debug mode, you still get leak detection, but without the source-file line-number reporting described above.
注意
この演算子をオーバーライドする場合は、もオーバーライドする必要があり delete
ます。If you override this operator, you must also override delete
. 標準ライブラリ関数は使用しないで _new_handler
ください。Don't use the standard library _new_handler
function.
例Example
CObList::CObList
CAge
例で使用されているクラスの一覧については、「」を参照してください CObject
。See CObList::CObList
for a listing of the CAge
class used in the CObject
examples.
void* CAge::operator new(size_t nSize)
{
return malloc(nSize);
}
void* CAge::operator new(size_t nSize, LPCSTR lpszFileName, int nLine)
{
UNREFERENCED_PARAMETER(lpszFileName);
UNREFERENCED_PARAMETER(nLine);
return malloc(nSize);
}
CObject:: SerializeCObject::Serialize
アーカイブに対して、このオブジェクトの読み取りまたは書き込みを行います。Reads or writes this object from or to an archive.
virtual void Serialize(CArchive& ar);
パラメーターParameters
ar
CArchive
シリアル化または逆シリアル化するオブジェクト。A CArchive
object to serialize to or from.
RemarksRemarks
Serialize
シリアル化する各クラスのをオーバーライドします。Override Serialize
for each class that you intend to serialize. オーバーライドされたは、 Serialize
まず Serialize
基底クラスの関数を呼び出す必要があります。The overridden Serialize
must first call the Serialize
function of its base class.
また、クラス宣言でマクロを使用する必要があり、 DECLARE_SERIAL
実装でマクロを使用する必要があり IMPLEMENT_SERIAL
ます。You must also use the DECLARE_SERIAL
macro in your class declaration, and you must use the IMPLEMENT_SERIAL
macro in the implementation.
CArchive::IsLoading
または CArchive::IsStoring
を使用して、アーカイブが読み込み中であるか格納されているかを判断します。Use CArchive::IsLoading
or CArchive::IsStoring
to determine whether the archive is loading or storing.
Serialize
は、およびによって呼び出され CArchive::ReadObject
CArchive::WriteObject
ます。Serialize
is called by CArchive::ReadObject
and CArchive::WriteObject
. これらの関数は、 CArchive
挿入演算子 ( <<
) と抽出演算子 () に関連付けられてい >>
ます。These functions are associated with the CArchive
insertion operator ( <<
) and extraction operator ( >>
).
シリアル化の例については、「 オブジェクトのシリアル化」を参照してください。For serialization examples, see the article Serializing an Object.
例Example
CObList::CObList
CAge
すべての例で使用されるクラスの一覧については、「」を参照してください CObject
。See CObList::CObList
for a listing of the CAge
class used in all CObject
examples.
void CAge::Serialize(CArchive& ar)
{
CObject::Serialize(ar);
if(ar.IsStoring())
ar << m_years;
else
ar >> m_years;
}