欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
C++中子類(lèi)轉父類(lèi)和父類(lèi)轉子類(lèi)的方法

18.2. Run-Time Type Identification

18.2. 運行時(shí)類(lèi)型識別

Run-time Type Identification (RTTI) allows programs that use pointers or references to base classes to retrieve the actual derived types of the objects to which these pointers or references refer.

通過(guò)運行時(shí)類(lèi)型識別(RTTI),程序能夠使用基類(lèi)的指針或引用來(lái)檢索這些指針或引用所指對象的實(shí)際派生類(lèi)型。

RTTI is provided through two operators:

通過(guò)下面兩個(gè)操作符提供 RTTI:

  1. The typeid operator, which returns the actual type of the object referred to by a pointer or a reference

    typeid 操作符,返回指針或引用所指對象的實(shí)際類(lèi)型。

  2. The dynamic_cast operator, which safely converts from a pointer or reference to a base type to a pointer or reference to a derived type

    dynamic_cast 操作符,將基類(lèi)類(lèi)型的指針或引用安全地轉換為派生類(lèi)型的指針或引用。

These operators return dynamic type information only for classes with one or more virtual functions. For all other types, information for the static (i.e., compile-time) type is returned.

這些操作符只為帶有一個(gè)或多個(gè)虛函數的類(lèi)返回動(dòng)態(tài)類(lèi)型信息,對于其他類(lèi)型,返回靜態(tài)(即編譯時(shí))類(lèi)型的信息。



The RTTI operators execute at run time for classes with virtual functions, but are evaluated at compile time for all other types.

對于帶虛函數的類(lèi),在運行時(shí)執行 RTTI 操作符,但對于其他類(lèi)型,在編譯時(shí)計算 RTTI 操作符。

Dynamic casts are needed when we have a reference or pointer to a base class but need to perform operations from the derived class that are not part of the base class. Ordinarily, the best way to get derived behavior from a pointer to base is to do so through a virtual function. When we use virtual functions, the compiler automatically selects the right function according to the actual type of the object.

當具有基類(lèi)的引用或指針,但需要執行不是基類(lèi)組成部分的派生類(lèi)操作的時(shí)候,需要動(dòng)態(tài)的強制類(lèi)型轉換。通常,從基類(lèi)指針獲得派生類(lèi)行為最好的方法是通過(guò)虛函數。當使用虛函數的時(shí)候,編譯器自動(dòng)根據對象的實(shí)際類(lèi)型選擇正確的函數。

In some situations however, the use of virtual functions is not possible. In these cases, RTTI offers an alternate mechanism. However, this mechanism is more error-prone than using virtual member functions: The programmer mustknow to which type the object should be cast and must check that the cast was performed successfully.

但是,在某些情況下,不可能使用虛函數。在這些情況下,RTTI 提供了可選的機制。然而,這種機制比使用虛函數更容易出錯:程序員必須知道應該將對象強制轉換為哪種類(lèi)型,并且必須檢查轉換是否成功執行了。

 

Dynamic casts should be used with caution. Whenever possible, it is much better to define and use a virtual function rather than to take over managing the types directly.

使用動(dòng)態(tài)強制類(lèi)型轉換要小心。只要有可能,定義和使用虛函數比直接接管類(lèi)型管理好得多。



18.2.1. The dynamic_cast Operator

18.2.1. dynamic_cast 操作符

The dynamic_cast operator can be used to convert a reference or pointer to an object of base type to a reference or pointer to another type in the same hierarchy. The pointer used with a dynamic_cast must be validit must either be0 or point to an object.

可以使用 dynamic_cast 操作符將基類(lèi)類(lèi)型對象的引用或指針轉換為同一繼承層次中其他類(lèi)型的引用或指針。與dynamic_cast 一起使用的指針必須是有效的——它必須為0 或者指向一個(gè)對象。

Unlike other casts, a dynamic_cast involves a run-time type check. If the object bound to the reference or pointer is not an object of the target type, then thedynamic_cast fails. If adynamic_cast to a pointer type fails, the result of thedynamic_cast is the value 0. If adynamic_cast to a reference type fails, then an exception of typebad_cast is thrown.

與其他強制類(lèi)型轉換不同,dynamic_cast 涉及運行時(shí)類(lèi)型檢查。如果綁定到引用或指針的對象不是目標類(lèi)型的對象,則dynamic_cast 失敗。如果轉換到指針類(lèi)型的dynamic_cast 失敗,則 dynamic_cast 的結果是 0 值;如果轉換到引用類(lèi)型的dynamic_cast 失敗,則拋出一個(gè)bad_cast 類(lèi)型的異常。

The dynamic_cast operator therefore performs two operations at once. It begins by verifying that the requested cast is valid. Only if the cast is valid does the operator actually do the cast. In general, the type of the object to which the reference or pointer is bound isn't known at compile-time. A pointer to base can be assigned to point to a derived object. Similarly, a reference to base can be initialized by a derived object. As a result, the verification that thedynamic_cast operator performs must be done at run time.

因此,dynamic_cast 操作符一次執行兩個(gè)操作。它首先驗證被請求的轉換是否有效,只有轉換有效,操作符才實(shí)際進(jìn)行轉換。一般而言,引用或指針所綁定的對象的類(lèi)型在編譯時(shí)是未知的,基類(lèi)的指針可以賦值為指向派生類(lèi)對象,同樣,基類(lèi)的引用也可以用派生類(lèi)對象初始化,因此,dynamic_cast 操作符執行的驗證必須在運行時(shí)進(jìn)行。

Using the dynamic_cast Operator
使用 dynamic_cast 操作符

As a simple example, assume that Base is a class with at least one virtual function and that classDerived is derived fromBase. If we have a pointer to Base namedbasePtr, we can cast it at run time to a pointer toDerived as follows:

作為例子,假定 Base 是至少帶一個(gè)虛函數的類(lèi),并且 Derived 類(lèi)派生于 Base 類(lèi)。如果有一個(gè)名為 basePtr 的指向 Base 的指針,就可以像這樣在運行時(shí)將它強制轉換為指向 Derived 的指針:

     if (Derived *derivedPtr = dynamic_cast<Derived*>(basePtr))     {         // use the Derived object to which derivedPtr points     } else { // BasePtr points at a Base object         // use the Base object to which basePtr points     }

At run time, if basePtr actually points to a Derived object, then the cast will be successful, andderivedPtr will be initialized to point to theDerived object to whichbasePtr points. Otherwise, the result of the cast is 0, meaning thatderivedPtr is set to 0, and the condition in the if fails.

在運行時(shí),如果 basePtr 實(shí)際指向 Derived 對象,則轉換將成功,并且 derivedPtr 將被初始化為指向 basePtr 所指的 Derived 對象;否則,轉換的結果是 0,意味著(zhù)將derivedPtr 置為 0,并且if 中的條件失敗。

 

We can apply a dynamic_cast to a pointer whose value is 0. The result of doing so is 0.

可以對值為 0 的指針應用 dynamic_cast,這樣做的結果是 0。



By checking the value of derivedPtr, the code inside theif knows that it is operating on aDerived object. It is safe for that code to useDerived operations. If thedynamic_cast fails because basePtr refers to aBase object, then theelse clause does processing appropriate toBase instead. The other advantage of doing the check inside theif condition is that it is not possible to insert code between thedynamic_cast and testing the result of the cast. It is, therefore, not possible to use thederivedPtr inadvertently before testing that the cast was successful. A third advantage is that the pointer is not accessible outside theif. If the cast fails, then the unbound pointer is not available for use in later cases where the test might be forgotten.

通過(guò)檢查 derivedPtr 的值,if 內部的代碼知道它是在操作 Derived 對象,該代碼使用Derived 的操作是安全的。如果dynamic_castbasePtr 引用了 Base 對象而失敗,則else 子句進(jìn)行適應于Base 的處理來(lái)代替。在 if 條件內部進(jìn)行檢查的另一好處是,不可能在 dynamic_cast測試轉換結果之間插入代碼,因此,不可能在測試轉換是否成功之前不經(jīng)意地使用derivedPtr。第三個(gè)好處是,在if 外部不能訪(fǎng)問(wèn)該指針,如果轉換失敗,則在后面的忘了測試的地方,未綁定的指針是不可用的。

 

Performing a dynamic_cast in a condition ensures that the cast and test of its result are done in a single expression.

在條件中執行 dynamic_cast 保證了轉換和其結果測試在一個(gè)表達式中進(jìn)行。



Using a dynamic_cast and Reference Types
使用 dynamic_cast 和引用類(lèi)型

In the previous example, we used a dynamic_cast to convert a pointer to base to a pointer to derived. Adynamic_cast can also be used to convert a reference to base to a reference to derived. The form for this adynamic_cast operation is the following,

在前面例子中,使用了 dynamic_cast 將基類(lèi)指針轉換為派生類(lèi)指針,也可以使用 dynamic_cast 將基類(lèi)引用轉換為派生類(lèi)引用,這種dynamic_cast 操作的形式如下:

     dynamic_cast< Type& >(val)

where Type is the target type of the conversion, and val is an object of base class type.

這里,Type 是轉換的目標類(lèi)型,而 val 是基類(lèi)類(lèi)型的對象。

The dynamic_cast operation converts the operand val to the desired typeType& only ifval actually refers to an object of the type Type or is an object of a type derived fromType.

只有當 val 實(shí)際引用一個(gè) Type 類(lèi)型對象,或者 val 是一個(gè)Type 派生類(lèi)型的對象的時(shí)候,dynamic_cast 操作才將操作數val 轉換為想要的 Type& 類(lèi)型。

Because there is no such thing as a null reference, it is not possible to use the same checking strategy for references that is used for pointer casts. Instead, when a cast fails, it throws astd::bad_cast exception. This exception is defined in the typeinfo library header.

因為不存在空引用,所以不可能對引用使用用于指針強制類(lèi)型轉換的檢查策略,相反,當轉換失敗的時(shí)候,它拋出一個(gè) std::bad_cast 異常,該異常在庫頭文件typeinfo 中定義。

We might rewrite the previous example to use references as follows:

可以重寫(xiě)前面的例子如下,以便使用引用:

     void f(const Base &b)     {        try {            const Derived &d = dynamic_cast<const Derived&>(b);        // use the Derived object to which b referred        } catch (bad_cast) {            // handle the fact that the cast failed        }     }


18.2.2. The typeid Operator

18.2.2. typeid 操作符

The second operator provided for RTTI is the typeid operator. Thetypeid operator allows a program to ask of an expression: What type are you?

為 RTTI 提供的第二個(gè)操作符是 typeid 操作符。typeid 操作符使程序能夠問(wèn)一個(gè)表達式:你是什么類(lèi)型?

A typeid expression has the form

typeid 表達式形如:

     typeid(e)

where e is any expression or a type name.

這里 e 是任意表達式或者是類(lèi)型名。

If the type of the expression is a class type and that class contains one or more virtual functions, then the dynamic type of the expression may differ from its static compile-time type. For example, if the expression dereferences a pointer to a base class, then the static compile-time type of that expression is the base type. However, if the pointer actually addresses a derived object, then thetypeid operator will say that the type of the expression is the derived type.

如果表達式的類(lèi)型是類(lèi)類(lèi)型且該類(lèi)包含一個(gè)或多個(gè)虛函數,則表達式的動(dòng)態(tài)類(lèi)型可能不同于它的靜態(tài)編譯時(shí)類(lèi)型。例如,如果表達式對基類(lèi)指針解引用,則該表達式的靜態(tài)編譯時(shí)類(lèi)型是基類(lèi)類(lèi)型;但是,如果指針實(shí)際指向派生類(lèi)對象,則typeid 操作符將說(shuō)表達式的類(lèi)型是派生類(lèi)型。

The typeid operator can be used with expressions of any type. Expressions of built-in type as well as constants can be used as operands for thetypeid operator. When the operand is not of class type or is a class without virtual functions, then thetypeid operator indicates the static type of the operand. When the operand has a class-type that defines at least one virtual function, then the type is evaluated at run time.

typeid 操作符可以與任何類(lèi)型的表達式一起使用。內置類(lèi)型的表達式以及常量都可以用作 typeid 操作符的操作數。如果操作數不是類(lèi)類(lèi)型或者是沒(méi)有虛函數的類(lèi),則typeid 操作符指出操作數的靜態(tài)類(lèi)型;如果操作數是定義了至少一個(gè)虛函數的類(lèi)類(lèi)型,則在運行時(shí)計算類(lèi)型。

The result of a typeid operation is a reference to an object of a library type namedtype_info.Section 18.2.4 (p. 779) covers this type in more detail. To use thetype_info class, the library headertypeinfo must be included.

typeid 操作符的結果是名為 type_info 的標準庫類(lèi)型的對象引用,第 18.2.4 節將更詳細地討論這個(gè)類(lèi)型。要使用type_info 類(lèi),必須包含庫頭文件typeinfo。

Using the typeid Operator
使用 typeid 操作符

The most common use of typeid is to compare the types of two expressions or to compare the type of an expression to a specified type:

typeid 最常見(jiàn)的用途是比較兩個(gè)表達式的類(lèi)型,或者將表達式的類(lèi)型與特定類(lèi)型相比較:

     Base *bp;     Derived *dp;     // compare type at run time of two objects     if (typeid(*bp) == typeid(*dp)) {         // bp and dp point to objects of the same type     }     // test whether run time type is a specific type     if (typeid(*bp) == typeid(Derived)) {         // bp actually points to a Derived     }

In the first if, we compare the actual types of the objects to whichbp anddp point. If they both point to the same type, then the test succeeds. Similarly, the secondif succeeds ifbp currently points to a Derived object.

第一個(gè) if 中,比較 bp 所指對象與 dp 所指對象的實(shí)際類(lèi)型,如果它們指向同一類(lèi)型,則測試成功。類(lèi)似地,如果bp 當前指向Derived 對象,則第二個(gè) if 成功。

Note that the operands to the typeid are expressions that are objectswe tested*bp, notbp:

注意,typeid 的操作數是表示對象的表達式——測試 *bp,而不是 bp

     // test always fails: The type of bp is pointer to Base     if (typeid(bp) == typeid(Derived)) {          // code never executed     }

This test compares the type Base* to type Derived. These types are unequal, so this test will always failregardless of the type of the object to whichbp points.

這個(gè)測試將 Base* 類(lèi)型與 Derived 類(lèi)型相比較,這兩個(gè)類(lèi)型不相等,所以,無(wú)論bp 所指對象的類(lèi)型是什么,這個(gè)測試將問(wèn)題失敗。

 

Dynamic type information is returned only if the operand to typeid is an object of a class type with virtual functions. Testing a pointer (as opposed to the object to which the pointer points) returns the static, compile-time type of the pointer.

只有當 typeid 的操作數是帶虛函數的類(lèi)類(lèi)型的對象的時(shí)候,才返回動(dòng)態(tài)類(lèi)型信息。測試指針(相對于指針指向的對象)返回指針的靜態(tài)的、編譯時(shí)類(lèi)型。



If the value of a pointer p is 0, then typeid(*p) throws abad_typeid exception if the type ofp is a type with virtual functions. If the type ofp does not define any virtuals, then the value ofp is irrelevant. As when evaluating asizeof expression (Section 5.8, p.167) the compiler does not evaluate *p. It uses the static type ofp, which does not require that p itself be a valid pointer.

如果指針 p 的值是 0,那么,如果 p 的類(lèi)型是帶虛函數的類(lèi)型,則 typeid(*p) 拋出一個(gè)bad_typeid 異常;如果p 的類(lèi)型沒(méi)有定義任何虛函數,則結果與 p 的值是不相關(guān)的。正像計算表達式sizeof第 5.8 節)一樣,編譯器不計算*p,它使用 p 的靜態(tài)類(lèi)型,這并不要求 p 本身是有效指針。


18.2.3. Using RTTI

18.2.3. RTTI 的使用

As an example of when RTTI might be useful, consider a class hierarchy for which we'd like to implement the equality operator. Two objects are equal if they have the same value for a given set of their data members. Each derived type may add its own data, which we will want to include when testing for equality.

作為說(shuō)明何時(shí)可以使用 RTTI 的例子,考慮一個(gè)類(lèi)層次,我們希望為它實(shí)現相等操作符。如果兩個(gè)對象的給定數據成員集合的值相同,它們就相等。每個(gè)派生類(lèi)型可以增加自己的數據,我們希望在測試相等的時(shí)候包含這些數據。

Because the values considered in determining equality for a derived type might differ from those considered for the base type, we'll (potentially) need a different equality operator for each pair of types in the hierarchy. Moreover, we'd like to be able to use a given type as either the left-hand or right-hand operand, so we'll actually need two operators for each pair of types.

因為確定派生類(lèi)型的相等與確定基類(lèi)類(lèi)型的相等所考慮的值不同,所以對層次中的每一對類(lèi)型(潛在地)需要一個(gè)不同的相等操作符。而且,希望能夠使用給類(lèi)型作為左操作數或右操作數,所以實(shí)際上對每一對類(lèi)型將需要兩個(gè)操作符。

If our hierarchy has only two types, we need four functions:

如果類(lèi)層次中只有兩個(gè)類(lèi)型,就需要四個(gè)函數:

     bool operator==(const Base&, const Base&)     bool operator==(const Derived&, const Derived&)     bool operator==(const Derived&, const Base&);     bool operator==(const Base&, const Derived&);

But if our hierarchy has several types, the number of operators we must define expands rapidlyfor only 3 types we'd need 9 operators. If the hierarchy has 4 types, we'd need 16, and so on.

但是,如果類(lèi)層次中有幾個(gè)類(lèi)型,必須定義的操作符的數目就迅速擴大——僅僅 3 個(gè)類(lèi)型就需要 9 個(gè)操作符。如果類(lèi)層次有 4 個(gè)類(lèi)型,將需要 16 個(gè)操作符,以此類(lèi)推。

We might think we could solve this problem by defining a set of virtual functions that would perform the equality test at each level in the hierarchy. Given those virtuals, we could define a single equality operator that operates on references to the base type. That operator could delegate its work to a virtual equal operation that would do the real work.

也許我們認為可以通過(guò)定義一個(gè)虛函數集合來(lái)解決這個(gè)問(wèn)題,這些虛函數可以在類(lèi)層次中每一層執行相等測試。給定這些虛函數,可以定義單個(gè)相等操作符,操作基類(lèi)類(lèi)型的引用,該操作符可以將工作委派給可以完成實(shí)際工作的虛操作。

Unfortunately, virtual functions are not a good match to this problem. The trouble is deciding on the type for the parameter to theequal operation. Virtual functions must have the same parameter type(s) in both the base and derived classes. That implies that a virtualequal operation must have a parameter that is a reference to the base class.

但是,虛函數并不是解決這個(gè)問(wèn)題的好辦法。麻煩在于決定 equal 操作的形參的類(lèi)型。虛函數在基類(lèi)類(lèi)型和派生類(lèi)型中必須有相同的形參類(lèi)型,這意味著(zhù),虛equal 操作必須有一個(gè)形參是基類(lèi)的引用。

However, when we compare two derived objects, we want to compare data members that might be particular to that derived class. If the parameter is a reference to base, we can use only members that are present in the base class. We cannot access members that are in the derived class but not in the base.

但是,當比較兩個(gè)派生類(lèi)對象的時(shí)候,我們希望比較可能特定于派生類(lèi)的數據成員。如果形參是基類(lèi)的引用,就只能比較基類(lèi)中出現的成員,我們不能訪(fǎng)問(wèn)在派生類(lèi)中但不在基類(lèi)中出現的成員。

Thinking about the problem in this detail, we see that we want to return false if we attempt to compare objects of different types. Given this observation, we can now use RTTI to solve our problem.

仔細考慮這個(gè)問(wèn)題,我們看到,希望在試圖比較不同類(lèi)型的對象時(shí)返回假(false)。有了這個(gè)觀(guān)察,現在可以使用 RTTI 解決我們的問(wèn)題。

We'll define a single equality operator. Each class will define a virtualequal function that first casts its operand to the right type. If the cast succeeds, then the real comparison will be performed. If the cast fails, then theequal operation will return false.

我們將定義單個(gè)相等操作符。每個(gè)類(lèi)定義一個(gè)虛函數 equal,該函數首先將操作數強制轉換為正確的類(lèi)型。如果轉換成功,就進(jìn)行真正的比較;如果轉換失敗,equal 操作就返回false。

The Class Hierarchy
類(lèi)層次

To make the concept a bit more concrete, let's assume that our classes look something like:

為了使概念更清楚一點(diǎn),假定類(lèi)層次是這樣的:

     class Base {         friend bool operator==(const Base&, const Base&);     public:         // interface members for Base     protected:         virtual bool equal(const Base&) const;         // data and other implementation members of Base     };     class Derived: public Base {         friend bool operator==(const Base&, const Base&);     public:         // other interface members for Derived     private:         bool equal(const Base&) const;         // data and other implementation members of Derived     };

A Type-Sensitive Equality Operator
類(lèi)型敏感的相等操作符

Next let's look at how we might define the overall equality operator:

下面看看可以怎樣定義整體的相等操作符:

     bool operator==(const Base &lhs, const Base &rhs)     {        // returns false if typeids are different otherwise        // returns lhs.equal(rhs)        return typeid(lhs) == typeid(rhs) && lhs.equal(rhs);     }

This operator returns false if the operands are different types. If they are the same type, then it delegates the real work of comparing the operands to the appropriate virtualequal function. If the operands areBase objects, then Base::equal will be called. If they areDerived objects,Derived::equal is called.

如果操作數類(lèi)型不同,這個(gè)操作符就返回假;如果操作數類(lèi)型相同,它就將實(shí)際比較操作數的工作委派給適當的虛函數 equal。如果操作數是Base 對象,就調用Base::equal;如果操作數是 Derived 對象,就調用 Derived::equal。

The Virtual equal Functions
虛函數 equal

Each class in the hierarchy must define its own version of equal. The functions in the derived classes will all start the same way: They'll cast their argument to the type of the class itself:

層次中的每個(gè)類(lèi)都必須定義自己的 equal 版本。派生類(lèi)中的 equal 函數將以相同的方式開(kāi)始:它們將實(shí)參強制轉換為類(lèi)本身的類(lèi)型。

     bool Derived::equal(const Base &rhs) const     {        if (const Derived *dp                   = dynamic_cast<const Derived*>(&rhs)) {           // do work to compare two Derived objects and return result        } else           return false;     }

The cast should always succeedafter all, the function is called from the equality operator only after testing that the two operands are the same type. However, the cast is necessary so that the function can access the derived members of the right-hand operand. The operand is a Base&, so if we want to access members of theDerived, we must first do the cast.

這個(gè)強制轉換應該總是成功——畢竟,只有有測試了兩個(gè)操作數類(lèi)型相同之后,才從相等操作符調用該函數。但是,這個(gè)強制轉換是必要的,以便函數可以訪(fǎng)問(wèn)右操作數的派生類(lèi)成員。因為操作數是Base&,所以如果想要訪(fǎng)問(wèn)Derived 的成員,就必須首先進(jìn)行強制轉換。

The Base-Class equal Function
基類(lèi) equal 函數

This operation is a bit simpler than the others:

這個(gè)操作比其他的簡(jiǎn)單一點(diǎn):

     bool Base::equal(const Base &rhs) const     {          // do whatever is required to compare to Base objects     }

There is no need to cast the parameter before using it. Both *this and the parameter are Base objects, so all the operations available for this object are also defined for the parameter type.

使用形參之前不必強制轉換,*this 和形參都是 Base 對象,所以對形參類(lèi)型也定義了該對象可用的所有操作。

18.2.4. The type_info Class

18.2.4. type_info 類(lèi)

The exact definition of the type_info class varies by compiler, but the standard guarantees that all implementations will provide at least the operations listed inTable 18.2

type_info 類(lèi)的確切定義隨編譯器而變化,但是,標準保證所有的實(shí)現將至少提供表 18.2 列出的操作。

Table 18.2. Operations on type_info
表 18.2. type_info 的操作

The class also provides a public virtual destructor, because it is intended to serve as a base class. If the compiler wants to provide additional type information, it should do so in a class derived fromtype_info.

因為打算作基類(lèi)使用,type_info 類(lèi)也提供公用虛析構函數。如果編譯器想要提供附加的類(lèi)型信息,應該在 type_info 的派生類(lèi)中進(jìn)行。

The default and copy constructors and the assignment operator are all defined asprivate, so we cannot define or copy objects of typetype_info. The only way to createtype_info objects in a program is to use thetypeid operator.

默認構造函數和復制構造函數以及賦值操作符都定義為 private,所以不能定義或復制 type_info 類(lèi)型的對象。程序中創(chuàng )建type_info 對象的唯一方法是使用typeid 操作符。

The name function returns a C-style character string for the name of the type represented by thetype_info object. The value used for a given type depends on the compiler and in particular is not required to match the type names as used in a program. The only guarantee we have about the return fromname is that it returns a unique string for each type. Nonetheless, thename member can be used to print the name of a type_info object:

name 函數為 type_info 對象所表示的類(lèi)型的名字返回 C 風(fēng)格字符串。給定類(lèi)型所用的值取決于編譯器,具體來(lái)說(shuō),無(wú)須與程序中使用的類(lèi)型名字匹配。對name 返回值的唯一保證是,它為每個(gè)類(lèi)型返回唯一的字符串。雖然如此,仍可以使用name 成員來(lái)顯示 type_info 對象的名字:

     int iobj;     cout << typeid(iobj).name() << endl          << typeid(8.16).name() << endl          << typeid(std::string).name() << endl          << typeid(Base).name() << endl          << typeid(Derived).name() << endl;

The format and value returned by name varies by compiler. This program, when executed on our machine, generates the following output:

name 返回的格式和值隨編譯器而變化。在我們的機器上執行時(shí),這個(gè)程序產(chǎn)生下面的輸出:

     i     d     Ss     4Base     7Derived

 

The type_info class varies by compiler. Some compilers provide additional member functions that provide additional information about types used in a program. You should consult the reference manual for your compiler to understand the exact type_info support provided.

type_info 類(lèi)隨編譯器而變。一些編譯器提供附加的成員函數,那些函數提供關(guān)于程序中所用類(lèi)型的附加信息。你應該查閱編譯器的參考手冊來(lái)理解所提供的確切的type_info 支持。



本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
c++ RTTI(運行時(shí)類(lèi)型識別)
5.4 類(lèi)型轉換高級 (Advacned Class Type-casting)
C :從技術(shù)實(shí)現角度聊聊RTTI
C 關(guān)鍵字typeid
C++ 中四種cast比較(轉載)
C++中的RTTI機制詳解
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久