Thinking C ++ Volume 2

zhaozj2021-02-16  51

For most programs, the downward shape is not necessary, because in the object-oriented application, a lot of problems have been solved every day. However, checking an ability to derive type modeling is important to most utilities such as compilers, class browsers, and databases. C provides a Dynamic_cast operator to check the shape. The following program is to rewrite the last example of Dynamic_cast:

//: C08: Security.h

#ifndef security_h

#define security_h

#include

Class security {

PUBLIC:

Virtual ~ security () {}

}

Class stock: public security {};

Class Bond: Public security {};

Class Investment: Public security {

PUBLIC:

Void Special () {

Std :: cout << "Special Investment Function" << std :: end1;

}

}

Class Metal: Public Investment {};

#ENDIF // Security_H / / /: ~

//: c08: checkedcast2.cpp

// Uses rtti's dynamic_cast.

#include

#include "../purge.h"

#include "security.h"

Using namespace std;

Int main () {

Vector portfolio;

Portfolio.push_back (new metal);

Portfolio.push_back (new investment);

Portfolio.push_back (new bond);

Portfolio.push_back (new stock);

for (Vector :: itrator it =

Portfolio.begin ();

It! = portfolio.end (); IT) {

Investment * cm = Dynamic_cast (* it);

IF (cm)

Cm-> Special ();

Else

COUT << "not a investment" << endl;

}

COUT << "Cast from Intermediate Pointer:" << Endl;

Security * sp = new metal;

Investment * CP = Dynamic_cast (sp);

IF (cp) cout << "it's an investment" << endl;

Metal * MP = Dynamic_cast (sp);

IF (MP) Cout << "It's a metal too!" << endl;

Purge (Portfolio);

} ///: ~

This example is shorter, because most of the original code is used to check the shape. The target type of Dynamic_cast is placed in a spacuular, like other C new types of conversions, the object to be converted as an operand. If you need a safe downward shape Dynamic_CAST requires the type you use to support polymorphism. This has, in turn, requires at least one virtual function. Fortunately, Security's base class has a false patterned function, so we don't need to create additional functions to complete your work. Because Dynamic_CAST works in runtime, use virtual functions, it costs more than other new style. You can use Dynamic_CAST instead of the pointer by reference, but because there is no empty reference here, you need another method to understand whether the shape failed. This additional method is to capture the BAD_CAST exception, as follows:

//: C08: catchbadcast.cpp

#include

#include "security.h"

Using namespace std;

Int main () {

Metal M;

Security & s = m;

Try {

Investment & C = Dynamic_Cast (s);

COUT << "It's an investment" << endl;

} catch (BAD_CAST &) {

Cout << "s is not an investment type" << Endl;

}

Try {

Bond & B = Dynamic_Cast (s);

Cout << "IT's a bond" << Endl;

} catch (BAD_CAST &) {

Cout << "IT's not a bond type" << Endl;

}

} ///: ~

The BAD_CAST class is defined in the header file . As other standard libraries, declared in STD namespace.

转载请注明原文地址:https://www.9cbs.com/read-22922.html

New Post(0)