CPP06 1.0
読み取り中…
検索中…
一致する文字列を見つけられません
main.cpp ファイル

Main entry point for the type identification program. [詳解]

#include <cstdlib>
#include <ctime>
#include <iostream>
#include "Base.hpp"
main.cpp の依存先関係図:

[ソースコード]

関数

Basegenerate (void)
 Randomly creates an instance of A, B, or C.
 
void identify (Base *p)
 Identifies the actual type of an object via a pointer.
 
void identify (Base &p)
 Identifies the actual type of an object from a reference.
 
int main ()
 Main function to test the type identification functions.
 

詳解

Main entry point for the type identification program.

main.cpp に定義があります。

関数詳解

◆ generate()

Base * generate ( void  )

Randomly creates an instance of A, B, or C.

戻り値
A Base pointer to the newly created instance.

main.cpp28 行目に定義があります。

◆ identify() [1/2]

void identify ( Base p)

Identifies the actual type of an object from a reference.

This function determines the runtime type of the object p by attempting to dynamic_cast it to derived types A, B, and C. It uses a series of nested try-catch(...) blocks to handle cast failures without needing to include <typeinfo> to catch a specific std::bad_cast exception.

引数
pA reference to a Base object whose type will be identified.

main.cpp73 行目に定義があります。

◆ identify() [2/2]

void identify ( Base p)

Identifies the actual type of an object via a pointer.

Uses dynamic_cast on a pointer, which returns a null pointer on failure, to determine the object's runtime type.

引数
pA pointer to a Base object.

main.cpp51 行目に定義があります。

◆ main()

int main ( )

Main function to test the type identification functions.

Creates random objects and identifies their types using both pointer and reference-based identification functions.

戻り値
0 on success.

main.cpp100 行目に定義があります。